thorax232

Java - Quick Sort

Nov 13th, 2013
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.78 KB | None | 0 0
  1. public static long quickSort(int[] list) {
  2.     quickSortHelper(list, 0, list.length - 1);
  3.     return list;
  4. }
  5.  
  6. public static void quickSortHelper(int[] list, int bottom, int top) {
  7.     if(bottom < top) {
  8.         int midpoint = partition(list, bottom, top);
  9.         quickSortHelper(list, bottom, midpoint - 1);
  10.         quickSortHelper(list, midpoint + 1, top);
  11.     }
  12. }
  13.  
  14. protected static int partition(int[] list, int bottom, int top) {
  15.     int pivot = list[top];
  16.     int firstAfterSmall = bottom;
  17.     for(int i = bottom; i < top; i++) {
  18.         if(list[i] <= pivot) {
  19.             swap(list, firstAfterSmall, i);
  20.             return firstAfterSmall;
  21.         }
  22.     }
  23.     swap(list, firstAfterSmall, top);
  24.     return firstAfterSmall;
  25. }
  26.  
  27. protected static void swap(int[] list, int i, int j) {
  28.     int temp = list[i];
  29.     list[i] = list[j];
  30.     list[j] = temp;
  31. }
Advertisement
Add Comment
Please, Sign In to add comment