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 + offset >= t.size()) 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
shuffle_example
Boost phoenix e.g. 1 no functor
srednie
Merge problem
PalindromePair-s1
poker.hpp
LRU - Set Sol
temp_new
Tray
4149 coj WIP