[algorithm notes - fast sorting algorithm]

Quick Sort

Geesforgeks quick sort blog, including video, algorithm complexity analysis, C C++ Java Python C ාimplementation version


  • Pivot center
  • Partition partition
  • Divide and Conquer algorithm
    • For example: Quick Sort, Merge Sort
    • Select an element in array as pivot
    • Split pivot left and right into two sub array s
  • permutation

Algorithm steps

  1. Select pivot
    1. First element in array (index=0)
    2. Intermediate element (index = length/2)
    3. Last element (index = length - 1)
    4. Randomly select an element (index = random(0, length-1))
  2. Partition processing: process array so that the array on the left side of pivot is smaller than pivot, and the right side of pivot is larger than pivot
  3. Recursive processing: repeat 1 or 2 operations on the left and right arrays until the left and right arrays are not separable


pivot is the last element

/* C++ implementation of QuickSort */
#include <bits/stdc++.h> 
using namespace std;  
// A utility function to swap two elements  
void swap(int* a, int* b)  
    int t = *a;  
    *a = *b;  
    *b = t;  
/* This function takes last element as pivot, places  
the pivot element at its correct position in sorted  
array, and places all smaller (smaller than pivot)  
to left of pivot and all greater elements to right  
of pivot */
int partition (int arr[], int low, int high)  
    int pivot = arr[high]; // pivot  
    int i = (low - 1); // Index of smaller element  
    for (int j = low; j <= high - 1; j++)  
        // If current element is smaller than the pivot  
        if (arr[j] < pivot)  
            i++; // increment index of smaller element  
            swap(&arr[i], &arr[j]);  
    swap(&arr[i + 1], &arr[high]);  
    return (i + 1);  
/* The main function that implements QuickSort  
arr[] --> Array to be sorted,  
low --> Starting index,  
high --> Ending index */
void quickSort(int arr[], int low, int high)  
    if (low < high)  
        /* pi is partitioning index, arr[p] is now  
        at right place */
        int pi = partition(arr, low, high);  
        // Separately sort elements before  
        // partition and after partition  
        quickSort(arr, low, pi - 1);  
        quickSort(arr, pi + 1, high);  
/* Function to print an array */
void printArray(int arr[], int size)  
    int i;  
    for (i = 0; i < size; i++)  
        cout << arr[i] << " ";  
    cout << endl;  
// Driver Code 
int main()  
    int arr[] = {10, 7, 8, 9, 1, 5};  
    int n = sizeof(arr) / sizeof(arr[0]);  
    quickSort(arr, 0, n - 1);  
    cout << "Sorted array: \n";  
    printArray(arr, n);  
    return 0;  
// This code is contributed by rathbh****upendra 

Time Complexity

T(n) = T(k)+ T(n-k-1) + Θ(n)
  • T(k) T(n-k-1) is left-right recursive processing and Θ (n) is partition processing time-consuming
  • k is the number of elements less than pivot
  • Input dependent Array, and pivot selection strategy

The following analysis: select the last Element as Pivot

Θ (n), O(nLogn), meaning unknown, I will check later (todo: 20200118)

Worst Case

swap is required for each comparison

  • It is equivalent to that array has been sorted, which is the opposite of this sorting,
    • For example, select a decimal as pivot
T(n) = T(0) + T(n-1) + Θ(n)
T(n) = T(n-1) + Θ(n)
T(n) = n^2 

Best Case

No swap

 T(n) = 2T(n/2) + Θ(n)
 T(n) = thea(nLogn)

Average Case

It is difficult to consider all array permutations. Assume that O(1/9N) is on the left and O(10/9N) is on the right

 T(n) = T(n/9) + T(9n/10) + \Θ(n)
 T(n) =  O(nLogn)

Summary of time complexity

  1. The Worst Case time complexity of Quick Sort is n^2, which is more than Merge Sort and head sort.
  2. But Quick Sort is still faster than Merge Sort and Heap Sort in practice
    1. In real data (less worst cases) and most architectures (I guess: hardware acceleration of comparison replacement), the internal loop is more efficient
    2. Quick Sort can be realized in different ways (change the position of pivot to make Worst Case less likely)
182 original articles published, praised 31, visited 250000+
Private letter follow

Tags: less Java Python

Posted on Sat, 18 Jan 2020 00:59:36 -0500 by videxx