// quick sort class RecSortExample { public static void main (String[] args) { int[] list = {15,8,7,3,2,14,11,1,5,9,4,12,13,6,10}; System.out.println (); System.out.println ("Here is the original list of integers "); System.out.println (); for (int i = 0; i < list.length; i++) System.out.print (list[i] + " "); System.out.println (); quickSort (list, 0, list.length - 1); System.out.println (); System.out.println (); System.out.println ("Here is the sorted list of integers "); System.out.println (); for (int i = 0; i < list.length; i++) System.out.print (list[i] + " "); System.out.println (); } static int[] quickSort (int[] list, int low, int high) { if (low < high) { int mid = partition (list, low, high); quickSort (list, low, mid - 1); quickSort (list, mid + 1, high); } return list; } static int partition (int[] list, int start, int end) { // the first number in the list is the pivot int pivot = list[start]; do { // look for a number smaller than the pivot starting // from the end of the list while (start < end && list[end] >= pivot) { end--; } if (start < end) { // a smaller number found list[start] = list[end]; // now find a number larger than the pivot // from the start while (start < end && list[start] <= pivot) { start++; } if (start < end) { // a larger number found list[end] = list[start]; } } } while (start < end); // done -- move the pivot back to the array list[start] = pivot; return start; } // end partition }