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(cur > 1 && 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); } } // 1 // / \ // 2 3 // /\ /\ // 4 5 6 7 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(0); 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
cvcvcvcvv
Hello
Round prices
Dar
Continuous Sub Set with given sum
Find the max and min number in array
volatile thread-safe object
Policy based smart pointer
CIDP2k19
code