Home | Projects | Notes > Data Structures & Algorithms > Quick Sort: T = O(n logn) , S = O(n)

Quick Sort: T = O(n logn) , S = O(n)

 

Quick Sort

Algorithm

  1. Input: An array of elements to be sorted.

  2. Choose Pivot: Select a pivot element from the array. The pivot can be chosen using various methods, such as selecting the first, last, or middle element.

  3. Partitioning: Rearrange the elements in the array such that all elements less than the pivot are moved to its left and all elements greater than the pivot are moved to its right. The pivot element is now in its final sorted position.

  4. Recursion: Recursively apply quick sort to the subarrays on the left and right of the pivot. These subarrays are formed by the partitioning step.

  5. Combine: No combining step is needed, as the array is already sorted in place after the partitioning and recursive sorting steps.

  6. Base Case: The base case for the recursion is when the subarray has only one element or is empty. In such cases, the array is considered sorted.

Pseudo-code

 

Code (C++)

 

Code (C)