Untitled
unknown
c_cpp
a year ago
1.6 kB
0
Indexable
Never
#include <iostream> #include <vector> using namespace std; void sort_simple_select(vector<int> &a) { for (size_t i = 0; i < a.size(); i++) { int min_index = i; for (int j = i; j < a.size(); j++) { if (a[j] < a[min_index]) min_index = j; } swap(a[min_index], a[i]); } } void sort_cocktail(vector<int>& a) { for (size_t i = 0; i < a.size() - i; i++) { for (size_t j = i; j < a.size() - i - 1; j++) { if (a[j] > a[j + 1]) swap(a[j], a[j + 1]); } for (size_t j = a.size() - i - 2; j > i - 1; j--) { if (a[j] > a[j + 1]) swap(a[j], a[j + 1]); } } } void make_heap(vector<int>& a, int n, int root) { int largest = root; int left = 2 * root + 1; int right = left + 1; if (left < n && a[left] > a[largest]) largest = left; if (right < n && a[right] > a[largest]) largest = right; if (largest != root) { swap(a[root], a[largest]); make_heap(a, n, largest); } } void sort_heap(vector<int>& a, int n) { for (int i = n / 2 - 1; i >= 0; i--) make_heap(a, n, i); for (int i = n - 1; i > 0; i--) { swap(a[0], a[i]); make_heap(a, i, 0); } } int main() { vector<int> arr{ 80, 50, 96, 65, 69, 1, 9, 56, 1, 48, 83, 44, 28, 16, 15 }; // sort_simple_select(arr); // sort_shake(arr); sort_heap(arr, arr.size()); for (auto i : arr) cout << i << " "; }