The answer depends on the strategy for choosing pivot. In early versions of Quick Sort where the leftmost (or rightmost) element is chosen as a pivot, the worst occurs in the following cases.
1) Array is already sorted in the same order.
2) Array is already sorted in reverse order.
3) All elements are the same (a special case of cases 1 and 2)
Since these cases are very common to use cases, the problem was easily solved by choosing either a random index for the pivot, choosing the middle index of the partition, or (especially for longer partitions) choosing the median of the first, middle and last element of the partition for the pivot. With these modifications, the worst case of Quicksort has fewer chances to occur, but a worst case can still occur if the input array is such that the maximum (or minimum) element is always chosen as the pivot.
This is the worst case of quicksort where we choose highest element as pivot
C++
#include <iostream> #include <vector> using namespace std; void quickSort(vector< int > &arr, int low, int high) { if (low < high) { int pivot = high; // Always choose the highest element as pivot int i = low - 1; for ( int j = low; j <= high - 1; j++) { if (arr[j] <= arr[pivot]) { i++; swap(arr[i], arr[j]); } } swap(arr[i + 1], arr[pivot]); int p = i + 1; quickSort(arr, low, p - 1); quickSort(arr, p + 1, high); } } int main() { vector< int > arr = {5, 4, 3, 2, 1}; quickSort(arr, 0, arr.size() - 1); for ( int i = 0; i < arr.size(); i++) { cout << arr[i] << " " ; } return 0; } |
Java
import java.util.*; class Main { public static void quickSort(List<Integer> arr, int low, int high) { if (low < high) { int pivot = high; // Always choose the highest element as pivot int i = low - 1 ; for ( int j = low; j <= high - 1 ; j++) { if (arr.get(j) <= arr.get(pivot)) { i++; Collections.swap(arr, i, j); } } Collections.swap(arr, i + 1 , pivot); int p = i + 1 ; quickSort(arr, low, p - 1 ); quickSort(arr, p + 1 , high); } } public static void main(String[] args) { List<Integer> arr = new ArrayList<>(Arrays.asList( 5 , 4 , 3 , 2 , 1 )); quickSort(arr, 0 , arr.size() - 1 ); for ( int i = 0 ; i < arr.size(); i++) { System.out.print(arr.get(i) + " " ); } } } |
Python3
def quickSort(arr, low, high): if low < high: pivot = high # Always choose the highest element as pivot i = low - 1 for j in range (low, high): if arr[j] < = arr[pivot]: i + = 1 arr[i], arr[j] = arr[j], arr[i] arr[i + 1 ], arr[pivot] = arr[pivot], arr[i + 1 ] p = i + 1 quickSort(arr, low, p - 1 ) quickSort(arr, p + 1 , high) arr = [ 5 , 4 , 3 , 2 , 1 ] quickSort(arr, 0 , len (arr) - 1 ) for i in range ( len (arr)): print (arr[i], end = " " ) |
C#
using System; using System.Collections.Generic; class MainClass { public static void QuickSort(List< int > arr, int low, int high) { if (low < high) { int pivot = high; // Always choose the highest element as pivot int i = low - 1; for ( int j = low; j <= high - 1; j++) { if (arr[j] <= arr[pivot]) { i++; arr.Swap(i, j); } } arr.Swap(i + 1, pivot); int p = i + 1; QuickSort(arr, low, p - 1); QuickSort(arr, p + 1, high); } } static void Main( string [] args) { List< int > arr = new List< int >( new int [] {5, 4, 3, 2, 1}); QuickSort(arr, 0, arr.Count - 1); for ( int i = 0; i < arr.Count; i++) { Console.Write(arr[i] + " " ); } } } public static class ListExtensions { public static void Swap<T>( this List<T> list, int index1, int index2) { T temp = list[index1]; list[index1] = list[index2]; list[index2] = temp; } } |
Javascript
// Javascript code function quickSort(arr, low, high) { if (low < high) { let pivot = high; //Always choose the highest element as pivot let i = low - 1; for (let j = low; j < high; j++) { if (arr[j] <= arr[pivot]) { i++; [arr[i], arr[j]] = [arr[j], arr[i]]; } } [arr[i + 1], arr[pivot]] = [arr[pivot], arr[i + 1]]; let p = i + 1; quickSort(arr, low, p - 1); quickSort(arr, p + 1, high); } } arr = [5, 4, 3, 2, 1]; quickSort(arr, 0, arr.length - 1); let ans= "" ; for (let i = 0; i < arr.length; i++) { ans = ans + arr[i]+ " " ; } console.log(ans); |
1 2 3 4 5
Time complexity : O(n^2)
Auxiliary Space :O(n)
References:
http://en.wikipedia.org/wiki/Quicksort
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!