Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <functional>
- using namespace std;
- template<class T>
- int my_partition(vector<T>&lista, int low, int high, function<int(T& e1, T& e2)> Compare)
- {
- T pivot = lista[high]; // pivot
- int i = low - 1; // Index of smaller element
- for (int j = low; j <= high- 1; j++)
- {
- if (Compare(lista[j],pivot))
- {
- i++;
- T aux = lista.at(j);
- lista.at(j) = lista.at(i);
- lista.at(i) = aux;
- }
- }
- T aux = lista[i + 1];
- lista[i + 1] = lista[high];
- lista[high] = aux;
- return i + 1;
- }
- template <class T>
- void quickSort(vector<T>&lista, int low, int high, function<int(T& e1, T& e2)> Compare)
- {
- if (low < high)
- {
- int pi = my_partition<T>(lista, low, high, Compare);
- quickSort(lista, low, pi - 1, Compare);
- quickSort(lista, pi + 1, high, Compare);
- }
- }
- template <class T>
- void Sort(vector<T> &lista, function<int(T& e1, T& e2)> Compare)
- {
- int high=0, low=0;
- for(vector<int>::iterator it=lista.begin();it!=lista.end();it++)high++;
- quickSort<T>(lista, low, high-1, Compare);
- }
- int main()
- {
- vector<int> lista = { 52, 38, 76, 16, 5, 44, 13, 82, 12 };
- function<int(int& e1, int& e2)> f = [](int& e1, int& e2) { return e1>e2; };
- Sort<int>(lista, f);
- for (auto it : lista)
- {
- cout << it <<endl;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement