Understand the quick sort algorithm

    In this tutorial, you will learn how quick sort works. In addition, you will find examples using C language.
    Quicksort is an algorithm based on the divide and conquer method. It divides the array into sub-arrays and recursively calls these sub-arrays to sort the elements.

How does the quick sort algorithm work?
  1. Select the pivot element from the array. You can select any element from the array as the pivot element.
    Here, we take the rightmost (that is, the last element) of the array as the pivot element.
    Insert picture description here

  2. Elements smaller than the pivot element are placed on the left, and elements larger than the pivot element are placed on the right.
    Insert picture description here
    The above arrangement is achieved through the following steps.
    a. The pointer is fixed on the axis element. The pivot element is compared with the element indicated by the first index. If the element is larger than the pivot element, the second pointer will point to the element.
    b. Now, the pivot element is compared with other elements (the third pointer). If the current element is smaller than the pivot element, the smaller element is swapped with the larger element found previously.
    Insert picture description here
    c. This process continues until the last element.
    Finally, the pivot element is swapped with the second pointer.
    Insert picture description here
    d. Now, the left and right sub-parts of this axis element will be further processed in the following steps.

  3. Again, select the pivot elements for the left and right subsections respectively. In these subsections, the pivot element is placed in the correct position. Then, repeat step 2.
    Insert picture description here

  4. The subsections are again divided into smaller subsections until each subsection is composed of a single element.

  5. At this point, the array has been sorted.

Quick sort uses recursion to sort subsections

    Based on the divide and conquer method, the quick sort algorithm can be explained as:

  • Divide The
    axis is the dividing point, and the array is divided into several sub-parts. Elements smaller than the axis are placed on the left side of the axis, and elements larger than the axis are placed on the right side of the axis.
  • Control
    Divide and conquer again by selecting the axis element for the left and right subparts. This can be achieved by recursively passing the sub-parts into the algorithm.
  • Combination
    This step does not play an important role in quick sort. The array has been sorted at the end of the control step.

    With the help of the illustration below, you can understand how quick sort works.
Insert picture description here
    Use recursion to sort the elements to the left of the pivot element
Insert picture description here
    Use recursion to sort the elements to the right of the pivot element

Quick sort algorithm pseudo code
quickSort(array, leftmostIndex, rightmostIndex)
  if (leftmostIndex < rightmostIndex)
    pivotIndex <- partition(array,leftmostIndex, rightmostIndex)
    quickSort(array, leftmostIndex, pivotIndex)
    quickSort(array, pivotIndex + 1, rightmostIndex)

partition(array, leftmostIndex, rightmostIndex)
  set rightmostIndex as pivotIndex
  storeIndex <- leftmostIndex - 1
  for i <- leftmostIndex + 1 to rightmostIndex
  if element[i] < pivotElement
    swap element[i] and element[storeIndex]
    storeIndex++
  swap pivotElement and element[storeIndex+1]
return storeIndex + 1
C example
// Quick sort in C

#include <stdio.h>

// Function to swap position of elements
void swap(int *a, int *b) {
    
    
  int t = *a;
  *a = *b;
  *b = t;
}

// Function to partition the array on the basis of pivot element
int partition(int array[], int low, int high) {
    
    
  
  // Select the pivot element
  int pivot = array[high];
  int i = (low - 1);

  // Put the elements smaller than pivot on the left 
  // and greater than pivot on the right of pivot
  for (int j = low; j < high; j++) {
    
    
    if (array[j] <= pivot) {
    
    
      i++;
      swap(&array[i], &array[j]);
    }
  }

  swap(&array[i + 1], &array[high]);
  return (i + 1);
}

void quickSort(int array[], int low, int high) {
    
    
  if (low < high) {
    
    
    
    // Select pivot position and put all the elements smaller 
    // than pivot on left and greater than pivot on right
    int pi = partition(array, low, high);
    
    // Sort the elements on the left of pivot
    quickSort(array, low, pi - 1);
    
    // Sort the elements on the right of pivot
    quickSort(array, pi + 1, high);
  }
}

// Function to print eklements of an array
void printArray(int array[], int size) {
    
    
  for (int i = 0; i < size; ++i) {
    
    
    printf("%d  ", array[i]);
  }
  printf("\n");
}

// Driver code
int main() {
    
    
  int data[] = {
    
    8, 7, 2, 1, 0, 9, 6};
  int n = sizeof(data) / sizeof(data[0]);
  quickSort(data, 0, n - 1);
  printf("Sorted array in ascending order: \n");
  printArray(data, n);
}
The complexity of quicksort

    time complexity

  • Worst case complexity [Big-O]: O( n 2 n^2n2 )
    Occurs when the selected axis element is the largest or smallest element.
    This situation causes the pivot element to be at the end of the sorted array. One sub-array is always empty, and the other sub-array contains n-1 elements. Therefore, only call quick sort on this sub-array.
    However, for scattered axes, the quick sort algorithm has better performance.
  • Best-case complexity [large Ω]: O(n*log n)
    The best-case complexity is when the axis element is always the middle element or is close to the middle element.
  • Average situation complexity [big θ]: O(n*logn)
    When the above two situations do not occur, it is the average situation complexity.

    Space complexity The space complexity of
    quicksort is O(log n).

Application of Quick Sort Algorithm

    When the following situations occur, use the quick sort algorithm:

  • Programming language is good for recursion
  • Time complexity is important
  • Space complexity is important
Reference documents

[1]Parewa Labs Pvt. Ltd.Quicksort Algorithm[EB/OL].https://www.programiz.com/dsa/quick-sort,2020-01-01.

Guess you like

Origin blog.csdn.net/zsx0728/article/details/114836295