Home > other >  To print the number of steps this RandomQuickSort Takes. Java code
To print the number of steps this RandomQuickSort Takes. Java code

Time:05-15

I have this Java algorithm and I am having trouble printing the number of steps it takes to solve the sorting. Here is the code

    /* The main function that implements QuickSort()
    arr[] --> Array to be sorted,
    low --> Starting index,
    high --> Ending index */
    static void sort(int arr[], int low, int high)
    {
        if (low < high)
        {
            /* partIndex is partitioning index, arr[partIndex] is
            now at right place */
            int partIndex = partition(arr, low, high);
 
            // Recursively sort elements before
            // partition and after partition
            sort(arr, low, partIndex-1);
            sort(arr, partIndex 1, high);
        }
    }
 
    /*  print array of size n */
    static void printArray(int arr[])
    {
        int n = arr.length;
        for (int i = 0; i < n;   i)
            System.out.print(arr[i] " ");
        System.out.println();
    }
 
    // Driver Code
    public static void main(String args[])
    {
        int[] data = {12,9,4,99,120,1,3,10,23,45,75,69,31,88,101,14,29,91,2,0,77};
               
        System.out.println("Unsorted Array \n"   " "); 
        System.out.print(Arrays.toString(data)  "\n");
        
        int n = data.length;
        RandomQuicSort.sort(data, 0, n - 1);
 
        //sort(data, 0, n-1);
 
        System.out.println("Sorted array in ascending order");
        System.out.println(Arrays.toString(data) "\n"); 
        System.out.println("Sorting was completed in: " ); 
        
        printArray(data);
    }
}

This is the output.

Unsorted Array 
 
[12, 9, 4, 99, 120, 1, 3, 10, 23, 45, 75, 69, 31, 88, 101, 14, 29, 91, 2, 0, 77]
9   Was swapped with    120
1   Was swapped with    9
0   Was swapped with    1
3   Was swapped with    10
9   Was swapped with    10
23  Was swapped with    101
23  Was swapped with    75
14  Was swapped with    23
69  Was swapped with    77
31  Was swapped with    75
31  Was swapped with    45
91  Was swapped with    120
101 Was swapped with    120
Sorted array in ascending order
[0, 1, 2, 3, 4, 9, 10, 12, 14, 23, 29, 31, 45, 69, 75, 77, 88, 91, 99, 101, 120]

Sorting was completed in: 
0 1 2 3 4 9 10 12 14 23 29 31 45 69 75 77 88 91 99 101 120 

I would like to change the last line Sorting was completed in:to display the number of steps the algorithm took to put the array in ascending order. Not the sorted array like it is now. For example, if the algorithm took 30 steps, I need to display Sorting was completed in: 30 Steps!. I have tried printing class.display(); but it gives me an error message.

Please help me.

Thank you.

CodePudding user response:

Keep track of the invokations of the sort method with a static member and increment it by one each time the sort function is run like so

    class RandomQuicSort
{    
    // This Function helps in calculating
    // the inclusive high and low
    public static int stepsToSort = 0;

    static void random(int arr[],int low,int high)

Then at the end of your sort method just increment it like this

static void sort(int arr[], int low, int high)
{
    if (low < high)
    {
        /* partIndex is partitioning index, arr[partIndex] is
        now at right place */
        int partIndex = partition(arr, low, high);

        // Recursively sort elements before
        // partition and after partition
        stepsToSort  ;
        sort(arr, low, partIndex-1);
        sort(arr, partIndex 1, high);

     
    }
}
  • Related