Overview of Quick Sort
Quick sort is a divide-and-conquer sorting algorithm. It works by choosing a random element from the array called pivot and repeatedly partitioning an array into two smaller subarrays, one containing all the elements smaller than the pivot element, and the other containing all the elements larger than the pivot element. The pivot element is then placed in the correct position in the sorted array. This process is repeated recursively until the array is sorted.
Intuition ๐ค
Well, this is not exactly how quick sort works but this is primarily the main intuition or inspiration for the Quick Sort algorithm.
Suppose we need to sort the array [4, 6, 2, 5, 7, 8, 1, 3]
The array contains elements from 1 to 8.
Let's choose the first element of the array i.e. 4 as the pivot.
Now we need to place 4 at its correct position and it should be done in such a way that all the elements smaller than 4 should lie left to it and greater than 4 should lie right to it.
Let's do it : [4, 6, 2, 5, 7, 8, 1, 3]
First place 4 at its correct position
[5, 6, 2, 4, 7, 8, 1, 3]
Now 5, 6 are greater than 4 and 1, 3 are less so let's just swap them
[1, 3, 2, 4, 7, 8, 5, 6]
Now all the elements left to 4 are smaller than 4 and right to 4 are greater than 4
Now we can recursively sort the subarrays [1, 3, 2] and [7, 8, 5, 6] with the same technique.
How Quick Sort Works ๐ง
The steps involved in implementing Quick Sort can be described as follows:
Choosing a Pivot: The first step is to select a pivot element from the array. The choice of a pivot greatly affects the efficiency of the algorithm. Typically, the pivot is selected as the first or last element of the array, but other strategies, such as selecting the middle or a random element, can also be employed.
Partitioning the Array ๐: Once the pivot is chosen, the array is partitioned into two subarrays: one containing elements smaller than the pivot and the other containing elements greater than the pivot. This process is known as partitioning. To achieve this, Quick Sort utilizes two pointers: one starting from the left end of the array and the other from the right end. The pointers move towards each other until they identify elements that need to be swapped.
Recursive Sorting: After partitioning the array, we recursively apply the same steps to the subarrays created. This involves selecting a pivot for each subarray, partitioning it, and continuing the process until the subarrays become trivially small (usually when they contain a single element). At this point, the array is already sorted.
Combining Subarrays โค๏ธโ๐ฉน: As the recursion unwinds, the sorted subarrays are combined to form the final sorted array.
Let's write PseudoCode ๐ง๐ปโ๐ป
First, let's focus on writing pseudocode for the partition part. It will take two parameters l and h standing for low and high.
Partition(l,h)
{
pivot=A[l]; //initializing pivot as the first element of array
i=l; j=h; //initializing two pointer i and j
while(i < j)
{ do{
i++;
} while (a[i] < pivot);
//increments i till it finds a value greater than the pivot
do{
j--;
} while (a[j] < pivot);
//decrements j till it finds a value smaller than the pivot
if(i < j)
swap(A[i], A[j]);
// swapping i and j in order to place where the should be.
// as value at i is greater than the pivot so it should be at right.
// Similarly the value at j should be at left. So swap them.
}
swap(A[l], A[j]);
return j;
// this part will be executed when i > j
// this means the spot at which pivot should lie is found and swap pivot with index j
// this will finally return the position of pivot
}
That was just ๐ค partitioning. Now let's look into QuickSort.
QuickSort(l, h)
{
if(l < h)
{
j = partition(l, h); // partition(l, h) gives the places the pivot element at its correct place and returns the index which is stored in var j
QuickSort(l, j); // for sorting the left sub array
QuickSort(j+1, h); // for sorting the right sub array
}
}
Performance Analysis
Advantages of Quick Sort
Quick sort is a very efficient sorting algorithm. It has an average-case time complexity of O(n log n) assuming the pivot places at the middle.
Quick sort is also a recursive algorithm, which means that it can be implemented in a very natural way.
Disadvantages of Quick Sort
Quick sort has a worst-case time complexity of O(n^2). This means that if the array is already sorted, or if the array is in reverse order, quick sort will take quadratic time to sort it ๐ข. Quick sort is also not a stable algorithm, which means that it does not preserve the original order of equal elements.
When to Use Quick Sort
Quick sort is a good choice for sorting large arrays. It is also a good choice for sorting arrays that are already sorted or in reverse order. Quick sort should not be used for sorting small arrays, as other sorting algorithms, such as insertion sort, will be more efficient.
Conclusion
Quick sort is a powerful and efficient sorting algorithm that is well-suited for a variety of applications. However, it is important to be aware of its limitations, such as its worst-case time complexity and its lack of stability.
Hope you have learned something from this blog. Go and play with some small arrays today and see the magic. We will be bringing much more blogs related to DSA. Don't forget to share your experience and any questions in the comments below. Happy coding! ๐ง๐ปโ๐ป