#include <iostream> #include <vector>
template <typename T> int partition(std::vector<T>& arr, int low, int high) { T pivot = arr[high]; int i = (low - 1);
for (int j = low; j <= high - 1; j++) { if (arr[j] <= pivot) { i++; std::swap(arr[i], arr[j]); } } std::swap(arr[i + 1], arr[high]); return (i + 1); }
template <typename T> void quickSort(std::vector<T>& arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high);
quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } }
template <typename T> void printArray(const std::vector<T>& arr) { for (int i = 0; i < arr.size(); i++) std::cout << arr[i] << " "; std::cout << std::endl; }
int main() { std::vector<int> intArray = {10, 7, 8, 9, 1, 5}; std::vector<double> doubleArray = {10.5, 7.2, 8.9, 9.1, 1.5, 5.3};
std::cout << "Original int array: "; printArray(intArray);
std::cout << "Sorted int array: "; quickSort(intArray, 0, intArray.size() - 1); printArray(intArray);
std::cout << "Original double array: "; printArray(doubleArray);
std::cout << "Sorted double array: "; quickSort(doubleArray, 0, doubleArray.size() - 1); printArray(doubleArray);
return 0; }
|