Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
HeapSort
Language:
Ada
Assembly
Bash
C#
C++ (gcc)
C++ (clang)
C++ (vc++)
C (gcc)
C (clang)
C (vc)
Client Side
Clojure
Common Lisp
D
Elixir
Erlang
F#
Fortran
Go
Haskell
Java
Javascript
Kotlin
Lua
MySql
Node.js
Ocaml
Octave
Objective-C
Oracle
Pascal
Perl
Php
PostgreSQL
Prolog
Python
Python 3
R
Rust
Ruby
Scala
Scheme
Sql Server
Swift
Tcl
Visual Basic
Layout:
Vertical
Horizontal
//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]; }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.44 sec, absolute running time: 0.15 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.6 sec
edit mode
|
history
|
discussion
5 3 2 4 10 1 7 9 8 6 1 2 3 4 5 7 8 9 10 6