Improvement on the Quick Sort Algorithm
Last Updated :
22 Apr, 2021
Prerequisite: QuickSort Algorithm
The quicksort algorithm discussed in this article can take O(N2) time in the worst case. Hence, certain variations are needed which can efficiently partition the array and rearrange the elements around the pivot.
Single Pivot Partitioning: In single pivot partitioning the array A[] can be divided into {A[p], A[p + 1], A[p + 2], …, A[r]} into two parts A[p…q] and A[q+1…r] such that:
- All elements in the first partition, A[p…q] are lesser than or equal to the pivot value A[q].
- All elements in the second partition, A[q+1…r] are greater than or equal to the pivot value A[q].
- After that, the two partitions are treated as independent input arrays and fed themselves to the Quick Sort Algorithm.
Lomuto's Partitioning: This is the simplest partitioning procedure to implement. Starting from the leftmost element and keeping track of the index of smaller (or equal to) elements as i. While traversing, if a smaller element is found, swap the current element with A[i]. Otherwise, ignore the current element.
It is discussed in detail in this article.
Hoare’s Partitioning: It works by initializing two indexes that start at two ends, the two indexes move toward each other until an inversion is (A smaller value on the left side and a greater value on the right side) found. When an inversion is found, two values are swapped and the process is repeated. Hoare’s Partition Scheme is more efficient than Lomuto’s Partition Scheme because it does three times fewer swaps on average, and it creates efficient partitions even when all values are equal.
The comparison of Lomuto's and Hoare's Partitioning is discussed in detail here.
Random Pivoting: In the above two approaches, the idea is to take the first or the last element of the unexplored array and find the correct position of the element in the array considering it as the pivot. If the array is already sorted in either increasing or decreasing order, and the first element is always chosen as a pivot, then it will be always the maximum or minimum element among the unexplored array elements. It will reduce the size of the unexplored array by 1 only and thus can cause the quick sort algorithm to perform the worst-case time complexity of O(N2).
Instead of picking the first element as pivot every time, if we pick a random element from the unexplored array and swap it with the first element and then perform the partitioning procedure (any one of the above two), then it will improve the expected or average time complexity to O(N*log N). The Worst-Case complexity is still O(N2). Though, when the array is already sorted, the worst case is avoided, but the worst case is hit when the array contains many repeated elements.
To know how a randomized algorithm improves the average case time complexity, please refer to this article.
Performance with Repeated Elements: Consider an array arr[] = [3, 3, 3, 3, 3, 3], i.e. array has all (or many) equal elements. On partitioning this array with the single-pivot partitioning scheme, we'll get two partitions. The first partition will be empty, while the second partition will have (N - 1) elements. Further, each subsequent invocation of the partition procedure will reduce the input size by only one. Since the partition procedure has O(N) complexity, the overall time complexity will be O(N2). This is the worst-case scenario when the input array has many repeated elements.
To reduce the worst-case scenarios when repeated elements are in large numbers, instead of a single-pivot partitioning scheme, we can implement a three-way partitioning scheme.
Three-way Partitioning: To efficiently sort an array having a high number of repeated keys, we can place keys in the right position when we first encounter them. So three-way partitioning of the array consists of the following three partitions:
- The left-most partition contains elements that are strictly less than the pivot.
- The middle partition contains all elements which are equal to the pivot.
- The right-most partition contains all elements which are strictly greater than the pivot.
Dutch National Flag (DNF) Sorting Algorithm: It can categorize all the numbers of an array into three groups with respect to a given pivot:
- The Lesser group contains all elements that are strictly lesser than the pivot.
- The Equal group contains all elements that are equal to the pivot.
- The Greater group contains all elements that strictly greater than the pivot.
To solve the DNF problem, pick the first element as the pivot, and scan the array from left to right. As we check each element, we move it to its correct group, namely Lesser, Equal, and Greater.
To learn the implementation of the DNF Sorting algorithm, please refer to this article.
Bentley-McIlroy's Optimized 3-Way Partitioning: This algorithm is an iteration-based partitioning scheme. All the elements of the array are unexplored initially. Start exploring the elements of the array from the left and right directions. After each iteration, it can be visualized that the array as a composition of five regions:
- Extreme two ends are regions that have elements equal to the pivot value.
- The unexplored region stays in the center and its size keeps on shrinking with each iteration.
- On the left side of the unexplored region are elements lesser than the pivot value.
- On the right side of the unexplored region are elements greater than the pivot value.
As the iteration procedure continues, the unexplored region of the array keeps on decreasing, and finally, the array will have no unexplored element remaining.
Now, since the elements equal to the pivot element is greater than elements in the Less region and less than elements in the greater region, they need to be moved to the center between the lesser and greater region. They are brought to the center by swapping with elements from lesser and greater regions.
Finally, the problem reduces to sort the elements in a lesser and greater region using the same approach again until the entire array becomes sorted.
Analysis of Three-Way Partitioning: In general, the Quick Sort Algorithm has an average-case time complexity of O(N*log(N)) and worst-case time complexity of O(N2). With a high number of duplicate keys, we always get the worst-case performance with the trivial implementation of Quick Sort.
However, using three-way partitioning reduces the impact of numerous duplicate keys. In fact, an array with all elements sorts in O(N2) time in single-pivot partitioning while it sorts in O(N) (linear) time complexity in three-way partitioning. Thus, what used to be worst-case turns out to be the best-case scenario for three-way partitioning.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read
Sorting Algorithms A Sorting Algorithm is used to rearrange a given array or list of elements in an order. For example, a given array [10, 20, 5, 2] becomes [2, 5, 10, 20] after sorting in increasing order and becomes [20, 10, 5, 2] after sorting in decreasing order. There exist different sorting algorithms for differ
3 min read