Quicksort
Let's now study the famous quicksort algorithm!
Introduction
Invented in 1959/60 by the Turing award-winning British computer scientist, Sir Charles Antony Richard Hoare, Quicksort is one of the top 10 most influential algorithms of the 20th century. It is another recursive divide and conquer algorithm.
Quicksort is the fastest known comparison-based sorting algorithm for arrays in the average case.
Caveat: Merge sort works better on linked lists, and there are other non-comparison based algorithms that outperform Quicksort.
Pseudocode
-
Start with an array of n elements
-
Choose a pivot element from the array to be sorted
-
Partition the array into 2 unsorted subarrays, such that all elements in one subarray are less than the pivot and all the elements in the other subarray are greater than the pivot
-
Elements that are equal to the pivot can go in either subarray.
-
Sort each subarray recursively to yield two sorted subarrays
-
Concatenate the two sorted subarrays and the pivot to yield one sorted array.
Level up your interview prep. Join Educative to access 70+ hands-on prep courses.