C++ Program for Quick Sort
Quick Sort is one of the most efficient sorting algorithms available to sort the given dataset. It is known for its efficiency in handling large datasets which made it a go-to choice for programmers looking to optimize their code.
In C++, the STL’s sort() function uses a mix of different sorting algorithms, including QuickSort, but we can’t choose which one it uses. For a pure Quick Sort implementation, you can use the C function qsort(), but it might not work with STL containers. The best approach is to manually implement it.
What is Quick Sort Algorithm?
QuickSort is a comparison-based sorting algorithm that uses the divide-and-conquer approach to sort an array or list of elements. It can be implemented using two functions:
- partition(): It is the key process in the quicksort algorithm. It involves selecting a pivot element and rearranging the array so that all elements smaller than the pivot are placed to its left, and the elements greater than the pivot are placed to its right. The point where the pivot is placed is called the partitioning index and it is returned to its caller quickSort().
- quickSort(): It is the main recursive function that implements the divide-and-conquer strategy. It divides the given array into two subarrays based on the partitioning index returned by partition() function. Then it keeps calling itself for these two subarrays until the whole array is sorted.
Quick sort is a highly efficient sorting algorithm. The C++ Course provides step-by-step instructions on implementing quick sort, allowing you to enhance your sorting skills.
C++ Program to Implement Quick Sort
The below program sorts a vector using quicksort algorithm:
// C++ Program to demonstrate how to implement the quick
// sort algorithm
#include <bits/stdc++.h>
using namespace std;
int partition(vector<int> &vec, int low, int high) {
// Selecting last element as the pivot
int pivot = vec[high];
// Index of elemment just before the last element
// It is used for swapping
int i = (low - 1);
for (int j = low; j <= high - 1; j++) {
// If current element is smaller than or
// equal to pivot
if (vec[j] <= pivot) {
i++;
swap(vec[i], vec[j]);
}
}
// Put pivot to its position
swap(vec[i + 1], vec[high]);
// Return the point of partition
return (i + 1);
}
void quickSort(vector<int> &vec, int low, int high) {
// Base case: This part will be executed till the starting
// index low is lesser than the ending index high
if (low < high) {
// pi is Partitioning Index, arr[p] is now at
// right place
int pi = partition(vec, low, high);
// Separately sort elements before and after the
// Partition Index pi
quickSort(vec, low, pi - 1);
quickSort(vec, pi + 1, high);
}
}
int main() {
vector<int> vec = {10, 7, 8, 9, 1, 5};
int n = vec.size();
// Calling quicksort for the vector vec
quickSort(vec, 0, n - 1);
for (auto i : vec) {
cout << i << " ";
}
return 0;
}
Output
1 5 7 8 9 10
Complexity Analysis of Quick Sort
- Time Complexity: O(n log n) on average and O(n2) in worst case
- Auxiliary Space: O(log n) on average and O(n) in the worst case.
Quick Sort in C++ – FAQs
How does the C++ implementation of Quick Sort work?
The C++ implementation selects a pivot, partitions the array into elements less than and greater than the pivot, and then recursively sorts the subarrays.
Is Quick Sort in C++ an in-place sorting algorithm?
Yes, the Quick Sort is an in-place sorting algorithm.
Can Quick Sort be used for large datasets?
Yes, Quick Sort is efficient for large datasets, especially when implemented with optimizations like choosing a good pivot.
Is Quick Sort stable in C++?
No, Quick Sort is not stable; equal elements may not retain their original order after sorting.