Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
HeapSort
//Title of this code #include <iostream> #include <vector> using namespace std; inline int parent(int i) { return i >> 1; } inline int left_child(int i) { return i << 1; } inline int right_child(int i) { return (i << 1) + 1; } void heapify(vector<int>& t, int cur, int offset) { if (cur > t.size() - offset) return; heapify(t, left_child(cur), offset); heapify(t, right_child(cur), offset); if(t[cur + offset - 1] < t[parent(cur) + offset - 1]) swap(t[cur + offset - 1], t[parent(cur) + offset - 1]); } void heap_sort(vector<int>& t) { for (int i = 0; i < t.size(); ++i) { heapify(t, 1, i); } } void print(vector<int>& t) { for (auto i = t.begin(); i != t.end(); ++i) cout << *i << " "; cout << endl; } int main() { vector<int> t; t.push_back(5); t.push_back(3); t.push_back(2); t.push_back(4); t.push_back(10); t.push_back(1); t.push_back(7); t.push_back(9); t.push_back(8); t.push_back(6); print(t); heap_sort(t); print(t); //std::cout << t[0]; }
run
|
edit
|
history
|
help
0
PRIx64 on gcc
СПКИ АП КЭП 2
articulation points and bridges
Dynamic Programming For Combinatorics - 1
Palindromo
barai_1
Zadanie Kalkulator z bajerami
bmp_lb
Test
Pairs with given sum