|
发表于 2020-10-26 07:07:09
|
显示全部楼层
- #include <iostream>
- #include <vector>
- using namespace std;
- template <typename T>
- void selectsort(vector<T>& arr)
- {
- T temp;
- for(int i=0; i<arr.size()-1; i++)
- {
- for(int j=i+1; j<arr.size(); j++)
- {
- if(arr[j] < arr[i])
- {
- temp = arr[j];
- arr[j] = arr[i];
- arr[i] = temp;
- }
- }
- }
- }
- template <typename T>
- void bubblesort(vector<T>& arr)
- {
- T temp;
- for(int i=0; i<arr.size(); i++)
- {
- for(int j=0; j<arr.size()-i-1; j++)
- {
- if(arr[j] > arr[j+1])
- {
- temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- }
- }
- }
- }
- template <typename T>
- void insertsort(vector<T>& arr)
- {
- T temp;
- for(int i=1; i<arr.size(); i++)
- {
- for(int j=i; j>0 && arr[j]<arr[j-1]; j--)
- {
- temp = arr[j];
- arr[j] = arr[j-1];
- arr[j-1] = temp;
- }
- }
- }
- template <typename T>
- void test(void (*func)(vector<T> &vct), vector<T> &vct)// vector<T>& arr vector<T> &vct
- {
- func(vct);
- }
- int main(void)
- {
- vector<int> vct {3, 4, 6, 0, -2, -2, 0, 0, -1, -9};
- test(insertsort, vct);
- for(int &e: vct)
- cout << e << ", ";
- cout << endl;
-
- return 0;
- }
复制代码 |
|