Coursera Learner working on a presentation with Coursera logo and
Coursera Learner working on a presentation with Coursera logo and

Quick Sort is quite similar to merge sort in the sense that this algorithm helps programmers divide and conquer. It chooses elements as a pivot, after which it create partitions in the array. Quick sort has numerous versions and each of them chooses the pivot differently. 

The partition() command is arguably the most important part of quick sort. Partition targets receive an array along with an x array element as pivot. You have to place the x in the right position, which is the sorted array, which will be followed by placing elements that are smaller than x before it. Finally, you have to place elements bigger than x after it. Make sure you do all of this in linear time. 

It is also worth keeping in mind that quick sort creates partitions in arrays, after which it calls itself in repetition twice for sorting the subarrays resulting from it. An algorithm like this comes in incredibly handy for large data sets. This is because O(n2) are its worst case complexities. The two initial terms are there for recursive calls, while the final term comes in handy in the partition process. The elements smaller than the pivot are represented by k. 

The amount of time quick sort usually takes depends on the partition strategy, along with the input array. 

The Three Cases of Quick Sort

Like various other sorting algorithms, quick sort also has case scenarios. Let us discuss them below:

Worst Case

This type of case takes place whenever the partition process chooses the smallest or greatest element as the pivot. Considering the partition strategy discussed earlier, where the final element is chosen as the pivot every time, the worst place scenario will end up occurring once the array gets sorted in descending or ascending order. The recurrence for the worst case is as follows:

 T(n) = T(0) + T(n-1) + (n)which is equivalent to  T(n) = T(n-1) + (n)

Best Case

Best place happens whenever the partition process chooses the middle element for pivot. The recurrence for best case is as follows:

T(n) = 2T(n/2) + (n)

Average Case

One must consider every array permutation and calculate the time every permutation takes for performing a case analysis. Of course this process can be quite complicated, even for experienced programmers. That said, taking the case into consideration when the partition places O(n/9) along with O(9n/10) elements can give you an estimate of average case. This case’s recurrence is as follows.

T(n) = T(n/9) + T(9n/10) + (n)

Common Questions asked about Quick Sort

While there are plenty of questions asked about quick sort, the ones mentioned below are arguably the most common:

Is quick sort a stable algorithm?

Well, quick sort’s default implementation does not have stability. That said, it is possible to add stability to sorting algorithms by considering the index as a parameter for comparison. 

What is three way quick sort?

Whenever a regular quick sort algorithm is in question, we choose an element to act as the pivot, create partition in the array. After which we recur for the sub arrays present on the pivot’s right and left sides. 

Is Quick Sort an in-place algorithm?

According to the in-place algorithm’s broad definition, quick sort indeed qualifies as this type of sorting algorithm. This is because it utilizes extra space to store recursive function calls. However, it does not manipulate the input in any way. 

Faster than Most Sorting Algorithms

While there is no denying that quick sort’s worst case time complexity is O(n2), more than heap sort, merge sort and numerous other sorting algorithms, quick sort happens to be incredibly fast. One of the reasons for this is that you can efficiently implement its inner loop in a several types of real world data and architectures. 

What’s more, you can implement quick sort in a variety of ways, simply by changing the pivot choice. Doing so minimizes the occurrence of worst case for any data type. That said, programmers generally prefer using merge sort whenever there is too much data, especially if it is inside external storage. 

What About Using Quick Sort in Linked Lists

When it comes to linked lists, things are quite different because of the differences present in the array’s memory allocation. Linked list nodes are quite different to arrays and often are not adjacent in the memory. What’s more linked lists allow you to enter items in the centre, something which is not possible in array. In such scenarios, programmers prefer using merge sort as it proves to be a more viable option compared to quick sort. 

 

Why Programmers Prefer Quick Sort to Sort Arrays

At its core, quick sort happens to be a simple in-place sort, which means it does not need extra storage. On the other hand, merge sort needs O(N) extra storage. For those who are wondering, the N denotes the size of the array, which could be incredibly extensive.  De-allocating and allocating the extra space required for merge sort raises the algorithm’s running time. 

Once we compare average complexity, it is easy to see that the average complexity of both of the sort types is O(NlogN). However, their constants tend to differ. When it comes to arrays, merge sort does not succeed because it uses the additional O(N) storage space. The most practical quick sort implementations make use of randomized version. The average time complexity of this version is O(nLogn). It is also worth keeping in mind that there is a possibility of worse case with the randomized version. However, it does not show up for specific patterns such as sorted array. 

The quick sort algorithm is quite cache-friendly due to its excellent locality of reference, particularly when one utilizes it for arrays. What’s more, quick sort also happens to be tail recursive, meaning that you have to perform tail call optimizations.