Quick Sort


The quicksort technique is done by separating the list into two parts. Initially, a pivot element is chosen by partitioning algorithm. The left part of the pivot holds the smaller values than the pivot, and right part holds the larger value. After partitioning, each separate lists are partitioned using the same procedure.

The complexity of Quicksort Technique

  • Time Complexity: O(n log n) for best case and average case, O(n^2) for the worst case.
  • Space Complexity: O(log n)

Input and Output

Input:
The unsorted list: 90 45 22 11 22 50
Output:
Array before Sorting: 90 45 22 11 22 50
Array after Sorting: 11 22 22 45 50 90

Algorithm

partition(array, lower, upper)

Input: The data set array, lower boundary and upper boundary

Output: Pivot in the correct position

Begin
   pivot := array[lower]
   start := lower and end := upper
   while start < end do
      while array[start] <= pivot AND start < end do
         start := start +1
      done

      while array[end] > pivot do
         end := end – 1
      done
      if start < end then
         swap array[start] with array[end]
   done

   array[lower] := array[end]
   array[end] := pivot
   return end
End

quickSort(array, left, right

Input − An array of data, and lower and upper bound of the array

Output − The sorted Array

Begin
   if lower < right then
      q = partition(arraym left, right)
      quickSort(array, left, q-1)
      quickSort(array, q+1, right)
End

Example

#include<iostream>
using namespace std;

void swapping(int &a, int &b) { //swap the content of a and b
   int temp;
   temp = a;
   a = b;
   b = temp;
}

void display(int *array, int size) {
   for(int i = 0; i<size; i++)
      cout << array[i] << " ";
   cout << endl;
}

int partition(int *array, int lower, int upper) {
   //Hoare partitioning technique to find correct location for pivot
   int pivot, start, end;
   pivot = array[lower];      //first element as pivot
   start = lower; end = upper;

   while(start < end) {
      while(array[start] <= pivot && start<end) {
         start++;      //start pointer moves to right
      }

      while(array[end] > pivot) {
         end--;      //end pointer moves to left
      }

      if(start < end) {
         swap(array[start], array[end]); //swap smaller and bigger element
      }
   }

   array[lower] = array[end];
   array[end] = pivot;
   return end;
}

void quickSort(int *array, int left, int right) {
   int q;

   if(left < right) {
      q = partition(array, left, right);
      quickSort(array, left, q-1);    //sort left sub-array
      quickSort(array, q+1, right);   //sort right sub-array
   }
}

int main() {
   int n;
   cout << "Enter the number of elements: ";
   cin >> n;
   int arr[n]; //create an array with given number of elements
   cout << "Enter elements:" << endl;

   for(int i = 0; i<n; i++) {
      cin >> arr[i];
   }

   cout << "Array before Sorting: ";
   display(arr, n);
   quickSort(arr, 0, n-1); //(n-1) for last index
   cout << "Array after Sorting: ";
   display(arr, n);
}

Output

Enter the number of elements: 6
Enter elements:
90 45 22 11 22 50
Array before Sorting: 90 45 22 11 22 50
Array after Sorting: 11 22 22 45 50 90 

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 15-Jun-2020

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements