赞
踩
- #include <iostream>
- #include <vector>
- #include <cstdlib>
- #include <ctime>
- void Qsort(std::vector<int>& arr, int low, int high);//形参绝对不能用unsigned!
- int Partition(std::vector<int>& arr, int low, int high);
- int main(void)
- {
- std::vector<int>arr(20);
- srand((unsigned int)time(nullptr));
- for (int i = 0; i != 20; ++i)
- arr[i] = (int)rand() % 100 + 1;
- for (int i = 0; i != 20; ++i)
- std::cout << arr[i] << ' ';
- std::cout << std::endl;
- Qsort(arr, 0, arr.size() - 1);
- for (int i = 0; i != 20; ++i)
- std::cout << arr[i] << ' ';
- std::cout << std::endl;
- return 0;
- }
- void Qsort(std::vector<int>& arr, int low, int high)
- {
- int pivot;
- if (low < high)//随处可见的low<high,保证程序正确运行
- {
- pivot = Partition(arr, low, high);//计算杻枢
- Qsort(arr, low, pivot - 1);//杻枢已经归位,无需对其排序
- Qsort(arr, pivot + 1
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。