Quick Sort
Quick Sort
Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data
into smaller arrays. A large array is partitioned into two arrays one of which holds values
smaller than the specified value, say pivot, based on which the partition is made and another
array holds values greater than the pivot value.
It is a faster and highly efficient sorting algorithm. This algorithm follows the divide and
conquer approach. Divide and conquer is a technique of breaking down the algorithms into
subproblems, then solving the subproblems, and combining the results back together to solve
the original problem.
Divide: In Divide, first pick a pivot element. After that, partition or rearrange the array into
two sub-arrays such that each element in the left sub-array is less than or equal to the pivot
element and each element in the right sub-array is larger than the pivot element.
Picking a good pivot is necessary for the fast implementation of quicksort. However, it is
typical to determine a good pivot. Some of the ways of choosing a pivot are as follows -
o Pivot can be random, i.e. select the random pivot from the given array.
o Pivot can either be the rightmost element of the leftmost element of the given array.
o Select median as the pivot element.
ALGORITHM:
Time Complexity
o Best Case Complexity - In Quicksort, the best-case occurs when the pivot element is
the middle element or near to the middle element. The best-case time complexity of
quicksort is O(n*logn).
o Average Case Complexity - It occurs when the array elements are in jumbled order
that is not properly ascending and not properly descending. The average case time
complexity of quicksort is O(n*logn).
o Worst Case Complexity - In quick sort, worst case occurs when the pivot element is
either greatest or smallest element. Suppose, if the pivot element is always the last
element of the array, the worst case would occur when the given array is sorted
Quicksort Advantages
This sorting technique is considered unstable since it doesn’t maintain the key-value
pairs initial order.
It isn’t as effective when the pivot element is the largest or smallest, or when all of the
components have the same size. The performance of the quicksort is significantly
impacted by these worst-case scenarios.
It’s difficult to implement since it’s a recursive process, especially if recursion isn’t
available.