#include <iostream>
#include <vector>
// Function to partition the array and return the index of the pivot element
int partition(std::vector<int>& arr, int low, int high) {
int pivot = arr[high];
int i = low - 1;
for (int j = low; j < high; j++) {
if (arr[j] < pivot) {
i++;
std::swap(arr[i], arr[j]);
}
}
std::swap(arr[i + 1], arr[high]);
return i + 1;
}
// Function to implement the quicksort algorithm
void quicksort(std::vector<int>& arr, int low, int high) {
if (low < high) {
int pivotIndex = partition(arr, low, high);
quicksort(arr, low, pivotIndex - 1);
quicksort(arr, pivotIndex + 1, high);
}
}
// Main function for testing
int main() {
std::vector<int> arr = {12, 7, 11, 5, 6, 2, 8};
int n = arr.size();
std::cout << "Original array: ";
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
quicksort(arr, 0, n - 1);
std::cout << "\nSorted array: ";
for (int i = 0; i < n; i++)
std::cout << arr[i] << " ";
return 0;
}