|  |  | 
 
 
 
    Here is the overall strategy of QuickSort: 
            
              QuickSort chooses an arbitrary value from somewhere in the list. A common location is the middle value of the list.
              This value becomes a decision point for roughly sorting the lis-t into two sublists, which are called the “left” and the “right” sublists. All the values smaller than the dividing value are placed in the left sublist, while all the values greater than the dividing value are placed in the right sublist.
              Each sublist is then recursively sorted with QuickSort.
              The termination of QuickSort occurs when a list of one value is obtained, which by definition is sorted.
    This is the code for quickSort: void quickSort (ArrayList <Comparable> list, int first, int last){int g = first, h = last;
    int midIndex = (first + last) / 2;Comparable dividingValue = list.get(midIndex);
 do{
 while (list.get(g).compareTo(dividingValue) < 0) g++;
 while (list.get(h).compareTo(dividingValue) > 0) h--;
 if (g <= h){
 //swap(list[g], list[h]);
 swap(list,g,h);
 g++;
 h--;
 }
 }while (g < h);
 if (h > first) quickSort (list,first,h);
 if (g < last) quickSort (list,g,last);
 }
Suppose we have the following list of 9 unsorted values in an array:
         17 22 34 28 19 84 11 92 1 
            
              The values received by the formal parameter listin the first call ofquickSortare a reference to an array, and the first and last cells of the array, 0 and 8. Variablefirstis initialized with the value 0, andlastis initialized with the value 8.
              The midIndexvalue is calculated as (0+8) / 2, which equals 4.
              The dividingValueis the value in location 4, list[4] = 19.
              The value of 19 will be our decision point for sorting values into two lists. The list to the left will contain all the values less than or equal to 19. The list to the right will contain the values larger than 19. Or simply put, small values go to the left and large values go to the right.
              The identifiers gandhwill be indices to locations in the list. Thewhileloops will movegandhuntil a value is found to be on the wrong side of the dividing value of 19. The g index is initialized with the value offirstand thehindex is initialized with the value oflast.
              The gindex starts at position 0 and moves until it “sees” that 22 is on the wrong side. Indexgstops at location 1.
              The hindex starts at position 8. Immediately it “sees” that the value 1 is on the wrong side.
              Since g <= h(1 <= 8), the values inlist[1]andlist[8]are swapped. After the values are swapped, indexgmoves one position to the right and indexhmoves one position to the left.
              The values of the pointers are now: g = 2, h = 7. We continue thedo-whileloop untilgandhhave passed each other, that is wheng > h. At this point, the lists will be roughly sorted, with values smaller than 19 on the left, and values greater than 19 on the right.
              If the left sublist has more than one value, (which is determined by the h > firstexpression), then a recursive call ofquickSortis made. This call ofquickSortwill send the index positions that define that smaller sublist.
              Likewise, if the right sublist has more than one value, quickSortis called again and the index positions that define that sublist are passed.   |