C++ Online Compiler
Example: Quick Sort Algorithm in C++
C
C++
C#
Java
Python
PHP
main.cpp
STDIN
Run
// Quick Sort Algorithm #include <iostream> #include <vector> #include <algorithm> // Required for std::swap // Function to print an array void printArray(const std::vector<int>& arr) { for (int x : arr) { std::cout << x << " "; } std::cout << std::endl; } // Function to partition the array on the basis of a pivot int partition(std::vector<int>& arr, int low, int high) { int pivot = arr[high]; // Choosing the last element as pivot int i = (low - 1); // Index of smaller element for (int j = low; j <= high - 1; j++) { // If current element is smaller than or equal to pivot if (arr[j] <= pivot) { i++; // Increment index of smaller element std::swap(arr[i], arr[j]); } } std::swap(arr[i + 1], arr[high]); return (i + 1); } // The main function that implements QuickSort // arr[] --> Array to be sorted, low --> Starting index, high --> Ending index void quickSort(std::vector<int>& arr, int low, int high) { if (low < high) { // pi is partitioning index, arr[pi] is now at right place int pi = partition(arr, low, high); // Separately sort elements before partition and after partition quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } int main() { std::vector<int> arr = {10, 7, 8, 9, 1, 5}; int n = arr.size(); std::cout << "Original array: "; printArray(arr); quickSort(arr, 0, n - 1); std::cout << "Sorted array: "; printArray(arr); return 0; }
Output
Clear
ADVERTISEMENTS