Don't like ads? PRO users don't see any ads ;-)
Guest

quicksort

By: Bladtman on Nov 3rd, 2011  |  syntax: Java  |  size: 0.91 KB  |  hits: 29  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.     private void sort(int[] arr, int a, int b)
  2.     {
  3.         if (a < b)
  4.         {
  5.             int i = a, j = b;
  6.             int x = arr[(i+j) /2];
  7.             do{
  8.                 for (int in : arr) {System.out.print(in + ", ");}
  9.                 System.out.print("\tp" + (i+j)/2 +"\ti" + i + "\tj" + j + "\ta" + a + "\tb" + b +"\t\t");
  10.                 for (int in = a; in <= b; in++) {System.out.print(arr[in] + ", ");}
  11.                 System.out.println();
  12.                
  13.                 while(arr[i] < x) {i++; comps++;}
  14.                 comps++;
  15.                 while(arr[j] > x) {j--; comps++;}
  16.                 comps++;
  17.                 if (i<=j)
  18.                 {
  19.                     swap(arr, i, j);
  20.                     i++;
  21.                     j--;
  22.                 }
  23.                 comps++;
  24.             } while(i<j);
  25.             sort (arr, a, j);
  26.             sort (arr, i, b);
  27.         }
  28.     }
  29.