- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- Related Articles
- Merge sort vs quick sort in Javascript
- Difference Between Quick Sort and Merge Sort
- Python Program for Iterative Quick Sort
- Java Program for Iterative Quick Sort
- How to implement quick sort in JavaScript?
- C# program to perform Quick sort using Recursion
- C++ Program to Implement Quick Sort Using Randomization
- Explain the quick sort technique in C language.
- Sorting an array of literals using quick sort in JavaScript
- C++ Program to Implement Quick Sort with Given Complexity Constraint
- C++ Program to Perform Quick Sort on Large Number of Elements
- Quick Fixes for Sore Muscles
- DevOps – A Solution To Quick Releases
- SaaS, PaaS, and IaaS - A Quick Comparison
- The formula for quick lime isCaCl2CaCO3Ca(OH)2CaO

Advertisements