1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #include <iostream> #include <vector> #include <algorithm> #include <map> using namespace std;
int partition(vector<int>& entry, int low, int high) { int pivot = entry[low]; int last_small = low; for (int i = low + 1; i <= high; i++) { if (entry[i] < pivot) { last_small++; swap(entry[i], entry[last_small]); } } swap(entry[low], entry[last_small]); return last_small; }
void recursive_quickSort(vector<int>& entry, int low, int high) { if (low < high) { int pivot = partition(entry, low, high); recursive_quickSort(entry, low, pivot - 1); recursive_quickSort(entry, pivot + 1, high); } }
void quickSort(vector<int>& entry) { recursive_quickSort(entry, 0, entry.size() - 1); }
int main() { int a[] = {3, 5, 7, 9, 2, 3, 1, 0, 7, 5, 4}; vector<int> va(a, a+11); cout << "Before QuickSort: " << endl; for (int i = 0; i < va.size(); i++) { cout << va[i] << " "; } cout << endl;
quickSort(va);
cout << "After QuickSort: " << endl; for (int i = 0; i < va.size(); i++) { cout << va[i] << " "; } cout << endl; return 0; }
|