Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Heap sort baaaad
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; 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); }
g++
Show compiler warnings
[
+
] Compiler args
[
+
]
Show input
Compilation time: 0.39 sec, absolute running time: 0.05 sec, cpu time: 0 sec, memory peak: 3 Mb, absolute service time: 0.52 sec
edit mode
|
history
|
discussion
3 6 4 1 2 5 3 6 5 1 2 4