Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Heap sort baaaad
//Title of this code #include <iostream> #include <vector> using namespace std; void heapify(vector<int>& t, int i, int heapLength) { int left = 2 * i; int right = (2 * i) + 1; int largest; if (left < t.size() && t[left] > t[i]) largest = left; else largest = i; if (right < t.size() && t[right] > t[largest]) largest=right; if (i != largest) { swap(t[i], t[largest]); heapify(t, largest, heapLength); } } void BuildMaxHeap(vector<int>& t, int heapLength) { for(int i = t.size() / 2; i > 0; --i) heapify(t, i, heapLength); } void HeapSort(vector<int>& t) { int heapLength = t.size() - 1; BuildMaxHeap(t, heapLength); for(int i = t.size() - 1; i > 1; --i) { swap(t[1], t[i]); --heapLength; heapify(t, 1, heapLength); } } void print(vector<int>& t) { for(int i = 0; i < t.size(); ++i) { cout << t[i] << " "; } cout << endl; } int main() { vector<int> t = {3,6,4,1,2,5}; print(t); HeapSort(t); print(t); }
run
|
edit
|
history
|
help
0
c++ mouse
Policy based smart pointer
Stream6
initializer_list example
PriQTel2
‘int R::print() const’ cannot be overloaded
Dijkstra
Display all prime numbers upto N without sieve
Estocasticos V1.1
test if nullptr