快排函数模板 #include <iostream> (30316)#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; }
点赞 评论

相关推荐

点赞 评论 收藏
分享
牛客网
牛客网在线编程
牛客网题解
牛客企业服务