Quick sort program in C++
In this post, you will learn about the Quick Sort and how to write a quick sort program using the C++ programming language.
Quick sort is widely used sorting algorithm, it is developed by C.A.R.
In quick sort, it first selects one element called a pivot. After that, it rearranges the elements in the array in such a way that all the elements less than the pivot element are appearing before the pivot and all the elements greater than the pivot are appearing after the pivot. After this, partitioning the pivot is in the final position.
Quick sort is the fastest algorithm and is the best choice. It is faster than other algorithms like bubble sort, selection sort, and insertion sort. But this can be complex on the flip side.
Quick Sort Example
These are the quick sorting techniques.
We choose the first element as a pivot. So set loc at 0 index, left at 0 index and right at 5th index.
Now scan from right to left until a[loc] < a[right], then decrease the value of right.
Since a[loc] > a[right], interchange the two values and set loc = right.
Now scan from left to right until a[loc] > a[left], then increment the value of left.
Since a[loc] < a[left], interchange the values and set loc = left.
Since from right to left, since a[loc] < a[right], decrease the value of right.
Start scanning from left to right. Since a[loc] > a[left], increment the value of left.
Since a[loc] > a[right], interchange the two values and set loc = right.
Complexity of Quick sort
Suppose n is the number of elements in an array.
Best Time Complexity - O(n log(n))
Worst Time Complexity - O(n2)
Quick sort program in C++
// C++ Implementation of the Quick Sort Algorithm.
#include <iostream>
using namespace std;
int partition(int arr[], int low, int high)
{
int pivot = arr[low];
int count = 0;
for (int i = low + 1; i <= high; i++) {
if (arr[i] <= pivot)
count++;
}
// Giving pivot element its correct position
int pivotIndex = low + count;
swap(arr[pivotIndex], arr[low]);
// Sorting left and right parts of the pivot element
int i = low, j = high;
while (i < pivotIndex && j > pivotIndex) {
while (arr[i] <= pivot) {
i++;
}
while (arr[j] > pivot) {
j--;
}
if (i < pivotIndex && j > pivotIndex) {
swap(arr[i++], arr[j--]);
}
}
return pivotIndex;
}
void quickSort(int arr[], int low, int high)
{
// base case
if (low >= high)
return;
// partitioning the array
int pi = partition(arr, low, high);
// Sorting the left part
quickSort(arr, low, pi - 1);
// Sorting the right part
quickSort(arr, pi + 1, high);
}
int main()
{
int arr[] = { 89, 53, 67, 91, 83, 70, 42, 49};
int n = 8;
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
return 0;
}
Output of the above code:
42 49 53 67 70 83 89 91
Related Articles
Queue implementation in c++Queue using linked list c++
Swapping of two numbers in C++
Generate random numbers in C++
GCD of two numbers in C++
Selection sort program in C++
Swapping of two numbers in C++
Generate random numbers in C++
C program for addition of two numbers
C program to calculate compound interest
C program to find the ASCII value of a character
C program to convert Decimal to Octal
C program to convert decimal to binary
Write a C program to calculate Simple Interest
C program to check whether a number is even or odd
C program to reverse a number
C program to check palindrome number
C program to check whether an alphabet is a vowel or consonant
Program to find square root of a number in C
C program to check whether a number is positive or negative