can you help me to insert counter for comparisons and swap in quicksort

0 nur syazwani · April 16, 2015
package quicksort;

public class QuickSort {
   
    private static int []a;
    public static void main(String[] args) {
       // Get a random generated array
        a = getArray();
       // prints the given array
        printArray();
        // sort the array
        sort();
        System.out.println("");
        //prints the sorted array
        printArray();
    }
    //This method sorts an array and integer
    public static void sort(){
        int left = 0;
        int right = a.length-1;
        quickSort(left,right);
    }
    
    //Ths=is method is used to sort the aray using quicksort algorithm.
    //It takes the left and the right end of the array as the two cursors.
    private static void quickSort(int left, int right){
        //If both cursor scanned the completea array quicksort exits
        if(left>= right)
            return;
        //For teh simplicity, we took the right most item of the array as a pivot
        int pivot = a
;
        int partition = partition(left, right, pivot);
        //Recursively, calls the quicksort with the different left and right parameters of the sub-array
        quickSort(0, partition-1);
        quickSort(partition+1, right);
    }

     // This method is used to partition the given array and returns the integer which points to the sorted pivot index
        private static int partition(int left,int right,int pivot){
        int leftCursor = left-1;
        int rightCursor = right;
        while(leftCursor < rightCursor){
               while(a[++leftCursor] < pivot);
               while(rightCursor > 0 && a[--rightCursor] > pivot);

               if(leftCursor >= rightCursor){
                break;
               }else{
                swap(leftCursor, rightCursor);
            }
       }

       swap(leftCursor, right);
       return leftCursor;
    }
     
    // This method is used to swap the values between the two given index
    public static void swap(int left,int right){
        int temp = a
;
        a
= a
;
        a
= temp;
   }

    public static void printArray(){
       for(int i : a){
            System.out.print(i+" ");
        }
    }

    public static int[] getArray(){
        int size=10;
        int []array = new int;
        int item = 0;
        for(int i=0;i<size;i++){
            item = (int)(Math.random()*100); 
            array = item;
       }
        return array;
    }

 }

Post a Reply

Replies

Oldest  Newest  Rating
+2 William Frost · May 10, 2015
That's Java, not Javascript.
  • 1

Javascript

107,327 followers
About

A scripting language that is added to standard HTML to create interactive effects, apps, games for the browser.

Links
Moderators