Quicksort

Quicksort is an extremely efficient sorting method that is a natural for implementation using recursion

In quicksort, we
  • Select a pivot value
  • Arrange the list such that
  • Everything to the left of the pivot has a smaller value
  • Everything to the right of the pivot has a larger value
  • Repeat the algorithm on the left and right sublists
  • 
      void quicksort(int list[], int start, int end)
      {
        int mid;
    
        if(start != end) {
    
          mid = split(list, start, end);
    
          quicksort(list, start, mid-1);
    
          quicksort(list, mid+1, end);
    
        }
      }
    
    
      int split(int list[], int start, int end)
      {
        int left = start;
        int right = end;
        int pivot = list[start];
        int temp;
    
        while(left < right) {
    
          // scan to the right looking for a value smaller than the pivot
          while(list[right] > pivot)
            right--;
    
          // scan to the left, looking for a value larger than the pivot
          while( (left < right) && (list[left] <= pivot) )
            left++;
    
          if(left < right) {
            // swap list[left] and list[right]
            temp = list[left];
            list[left] = list[right];
            list[right] = temp;
          }
        }      
    
        list[start] = list[right];
        list[right] = pivot;
    
        return(right);
    
      }