Data Structure & Algorithm in Java

⌘K
  1. Home
  2. Docs
  3. Data Structure & Alg...
  4. Sorting
  5. Quick Sort

Quick Sort

  • Quicksort is a sorting algorithm based on the divide and conquer algorithm 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.
  • Quick sort is a highly efficient sorting algorithm and is based on partitioning of array of data into smaller arrays.
  • Quicksort partitions an array and then calls itself recursively twice to sort the two resulting subarrays. This algorithm is quite efficient for large-sized data sets as its average and worst-case complexity are O(n2), respectively.
  • It is a divide-and-conquer algorithm that makes it easier to solve problems.
  • It is efficient on large data sets.
  • It has a low overhead, as it only requires a small amount of memory to function.
  • It has a worst-case time complexity of O(N2), which occurs when the pivot is chosen poorly.
  • It is not a good choice for small data sets.
  • It is not a stable sort, meaning that if two elements have the same key, their relative order will not be preserved in the sorted output in case of quick sort, because here we are swapping elements according to the pivot’s position (without considering their original positions).

→The key process in quicksort is a partition(). The target of partitions is to place the pivot (any element can be chosen to be a pivot) at its correct position in the sorted array and put all smaller elements to the left of the pivot, and all greater elements to the right of the pivot.

Partition is done recursively on each side of the pivot after the pivot is placed in its correct position and this finally sorts the array.

QuickSort2

There are many different choices for picking pivots:

  • Always pick the first element as a pivot.
  • Always pick the last element as a pivot(implemented below)
  • Pick a random element as a pivot.
  • Pick the middle as a pivot.

»The logic is simple, we start from the leftmost element and keep track of the index of smaller (or equal) elements as i. While traversing, if we find a smaller element, we swap the current element with arr[i]. Otherwise, we ignore the current element.

Let us understand the working of partition and the Quick Sort algorithm with the help of the following example:

Consider: arr[] = {10, 80, 30, 90, 40}.

  • Compare 10 with the pivot and as it is less than pivot arrange it accordingly.
Screenshot 2024 03 21 193713

Partition in Quicksort: Compare pivot with 10

  • Compare 80 with the pivot. It is greater than pivot.
Screenshot 2024 03 21 194041

Partition in Quicksort: Compare pivot with 80

  • Compare 30 with pivot. It is less than pivot so arrange it accordingly.
Screenshot 2024 03 21 193916

Partition in Quicksort: Compare pivot with 30

  • Compare 90 with the pivot. It is greater than the pivot.
Screenshot 2024 03 21 194224

Partition in Quicksort: Compare pivot with 90

  • Arrange the pivot in its correct position.
Screenshot 2024 03 21 194339

Partition in Quicksort: Place pivot in its correct position

»As the partition process is done recursively, it keeps on putting the pivot in its actual position in the sorted array. Repeatedly putting pivots in their actual position makes the array sorted.

Follow the below images to understand how the recursive implementation of the partition algorithm helps to sort the array.

  • Initial partition on the main array:
Screenshot 2024 03 21 194714

Quicksort: Performing the partition

Partitioning of the subarrays:

Screenshot 2024 03 21 194727

Quicksort: Performing the partition

Time Complexity:

  • Best Case: Ω (N log (N))
    The best-case scenario for quicksort occur when the pivot chosen at the each step divides the array into roughly equal halves.
    In this case, the algorithm will make balanced partitions, leading to efficient Sorting.
  • Average Case: θ ( N log (N))
    Quicksort’s average-case performance is usually very good in practice, making it one of the fastest sorting Algorithm.
  • Worst Case: O(N2)
    The worst-case Scenario for Quicksort occur when the pivot at each step consistently results in highly unbalanced partitions. When the array is already sorted and the pivot is always chosen as the smallest or largest element. To mitigate the worst-case Scenario, various techniques are used such as choosing a good pivot (e.g., median of three) and using Randomized algorithm (Randomized Quicksort ) to shuffle the element before sorting.
  • Auxiliary Space: O(1), if we don’t consider the recursive stack space. If we consider the recursive stack space then, in the worst case quicksort could make O(N).

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *