快排函数模板
#include <iostream> #include <vector> template <typename t=""> void quicksort(std::vector<t>& v, int left, int right) { if (left < right) { int i = left, j = right; T pivot = v[(left + right) / 2]; while (i <= j) { while (v[i] < pivot) i++; while (v[j] > pivot) j--; if (i <= j) { std::swap(v[i], v[j]); i++; j--; } } quicksort(v, left, j); quicksort(v, i, right); } } int main() { std::vector<int> v = {9, 3, 6, 1, 8, 4, 7, 5, 2}; quicksort(v, 0, v.size()-1); for (auto i : v) { std::cout << i << " "; } std::cout << std::endl; return 0; }</int></t></typename></vector></iostream>