Quick Sort
Quick Sort
AfterAcademy
Admin AfterAcademy
10 Aug 2020
Quick Sort
Difficulty: Hard
https://afteracademy.com/blog/quick-sort/ 1/10
1/2/25, 12:17 PM Quick Sort
Example 1
Example 2
Solution
As you might know for sorting or comparison-based sorting algorithm the
best time complexity that is possible is NlogN. Some of the algorithms that
provide this time complexity are merge sort and quicksort. You could read
about the merge sort algorithm and understand how the sorting takes place
and you will also get to know that the merge sort uses an auxiliary space
complexity of O(N).
We will discuss quick sort which guarantees with a very high probability of
time complexity of NlogN in average case but has N² in its worst case. Even
though the time complexity is N² in the worst case, It is mostly used by
various applications because its space complexity is O(1). This algorithm is
so much efficient in practical use cases that the sort function of most of the
languages uses quicksort.
Let us see how the quicksort algorithm works in practice for this array.
https://afteracademy.com/blog/quick-sort/ 2/10
1/2/25, 12:17 PM Quick Sort
[35, 33, 42, 10, 14, 19, 27, 44, 26, 31]
At each iteration of quicksort, we choose the pivot element and try to bring
it at its correct position. Let us see that in our implementation of quicksort,
we always choose the end element of the array as a pivot and try to bring it
at its correct position. What this means that we will try to bring all the
elements that are smaller than the pivot element to the left side of it and all
the elements greater than the pivot element to the right side of it.
The pivot element is the element that will try to be at the current position
is known as the pivot element.
In the above case, in the first iteration, we choose 31 as the pivot, now
10, 14, 19, 27, 26 will go to the left of 31 and 35, 33, 42, 44
will go to the right of 31 . Now you have two smaller unsorted arrays [10,
14, 19, 27, 26] 31 [35, 33, 42, 44] , so we can repeat the
process as the pivot element 31 is at its correct position.
Now if we go on, there will be the case when the size of the subarray
becomes 1, which will be considered as the base case.
If we follow the same procedure for example 2. Then each step will work like
this:
https://afteracademy.com/blog/quick-sort/ 3/10
1/2/25, 12:17 PM Quick Sort
Solution Steps
1. Find a pivot item in the array. This item is the basis for comparison
for a single round.
2. Start a pointer (the left pointer ) at the first item in the array.
3. Start a pointer (the right pointer ) at the last item in the array.
4. While arr[left pointer] < pivot value , move the left pointer
to the right. Continue until the arr[left pointer] >= pivot value.
7. Move the left pointer to the right by one and the right pointer
to the left by one.
Pseudo Code
Complexity Analyisis
start > end . Is this case possible? If yes, then think of such a case!
Can you write the recurrence relation for the quicksort algorithm?
Please comment down below if you have a better insight in the above approach.
https://afteracademy.com/blog/quick-sort/ 6/10
1/2/25, 12:17 PM Quick Sort
https://afteracademy.com/blog/quick-sort/ 7/10
1/2/25, 12:17 PM Quick Sort
https://afteracademy.com/blog/quick-sort/ 8/10
1/2/25, 12:17 PM Quick Sort
https://afteracademy.com/blog/quick-sort/ 9/10
1/2/25, 12:17 PM Quick Sort
https://afteracademy.com/blog/quick-sort/ 10/10