Advertisement
Guest User

cde

a guest
Apr 18th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.44 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <functional>
  4. using namespace std;
  5.  
  6.  
  7. template<class T>
  8. int my_partition(vector<T>&lista, int low, int high, function<int(T& e1, T& e2)> Compare)
  9. {
  10. T pivot = lista[high]; // pivot
  11. int i = low - 1; // Index of smaller element
  12.  
  13. for (int j = low; j <= high- 1; j++)
  14. {
  15.  
  16. if (Compare(lista[j],pivot))
  17. {
  18. i++;
  19. T aux = lista.at(j);
  20. lista.at(j) = lista.at(i);
  21. lista.at(i) = aux;
  22. }
  23. }
  24. T aux = lista[i + 1];
  25. lista[i + 1] = lista[high];
  26. lista[high] = aux;
  27. return i + 1;
  28. }
  29.  
  30. template <class T>
  31. void quickSort(vector<T>&lista, int low, int high, function<int(T& e1, T& e2)> Compare)
  32. {
  33. if (low < high)
  34. {
  35.  
  36. int pi = my_partition<T>(lista, low, high, Compare);
  37.  
  38. quickSort(lista, low, pi - 1, Compare);
  39. quickSort(lista, pi + 1, high, Compare);
  40. }
  41. }
  42.  
  43. template <class T>
  44. void Sort(vector<T> &lista, function<int(T& e1, T& e2)> Compare)
  45. {
  46. int high=0, low=0;
  47. for(vector<int>::iterator it=lista.begin();it!=lista.end();it++)high++;
  48. quickSort<T>(lista, low, high-1, Compare);
  49. }
  50.  
  51.  
  52. int main()
  53. {
  54.  
  55. vector<int> lista = { 52, 38, 76, 16, 5, 44, 13, 82, 12 };
  56. function<int(int& e1, int& e2)> f = [](int& e1, int& e2) { return e1>e2; };
  57. Sort<int>(lista, f);
  58. for (auto it : lista)
  59. {
  60. cout << it <<endl;
  61. }
  62. return 0;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement