Posted by: techGuy May 1, 2008
Java le DusPutra (tenson) diyo!
Login in to Rate this Post:     0       ?        

hey GC ,

did u solve it, if not here is the code, added swap and main and remove that generic thing, now works only with int.

Happy programming...

** Implements the quicksort algorithm. */

public class  QuickSort {
    public static void sort(int[] table) {      
        quickSort(table,0,table.length - 1);
    }
    private static  void quickSort(int[] table,int first, int last) {
        if (first < last) {
            int pivIndex = partition(table,first,last);
            quickSort(table,first,pivIndex - 1);
            quickSort(table,pivIndex +1,last);
        }
    }
    private static int partition(int[] table,int first,int last) {
        bubbleSort3(table,first,last);
        swap(table,first,(first+last)/2);
            int pivot = table[first];
        int up = first;
        int down = last;
        do {
            while ((up<last)&&(((Integer)pivot).compareTo(table[up]) >= 0)) {
                up++;
            }
            while (((Integer)pivot).compareTo(table[down]) <0) {
                down--;
            }
            if (up<down) {
                swap(table,up,down);
            }
        }
        while (up<down);
        swap(table,first,down);
        return down;
    }
   
        private static  void bubbleSort3(int[] table,int first,int last) {
            int middle = (first + last) / 2;
           
            if (((Integer)table[middle]).compareTo(table[first])<0) {
                swap(table,first,middle);
            }
            if (((Integer)table[last]).compareTo(table[middle])<0) {
                swap(table,middle,last);
            }
            if (((Integer)table[middle]).compareTo(table[first])<0) {
                swap(table,first,middle);
            }
        }
   
       private static  void swap(int[] table, int a, int b)
{
      
     int temp=table[a];
      table[a]=table[b];
     table[b]=temp;
}

public static void main(String args[])
 {
int [] a={6,4,2,1,4,3,5,7};
 sort(a);


   for(int i=0;i<a.length;i++)
       System.out.println(a[i]);

}


}

Read Full Discussion Thread for this article