TheAlgorithms/C++
1.0.0
All the algorithms implemented in C++
Toggle main menu visibility
Main Page
Related Pages
Topics
Namespaces
Namespace List
Namespace Members
All
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
z
Functions
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
z
Variables
Typedefs
Classes
Class List
Class Index
Class Hierarchy
Class Members
All
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
x
y
~
Functions
_
a
b
c
d
e
f
g
h
i
k
l
m
n
o
p
q
r
s
t
u
v
w
~
Variables
_
a
b
c
d
e
f
g
h
i
k
l
m
n
p
q
r
s
t
u
v
w
x
y
Typedefs
Related Symbols
Files
File List
File Members
All
_
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
q
r
s
t
u
w
z
Functions
_
a
b
c
d
e
f
g
h
i
j
l
m
n
o
p
q
r
s
t
u
z
Variables
Typedefs
Macros
Examples
▼
TheAlgorithms/C++
►
The Algorithms - C++
►
Contributor Covenant Code of Conduct
►
Code style convention
►
CONTRIBUTION GUIDELINES
►
Backtracking
Prime factorization
Guidelines for reviewers and maintainers
Todo List
►
Topics
►
Namespaces
►
Classes
▼
Files
▼
File List
►
backtracking
►
bit_manipulation
►
ciphers
►
cpu_scheduling_algorithms
►
data_structures
►
divide_and_conquer
►
dynamic_programming
►
games
►
geometry
►
graph
►
graphics
►
greedy_algorithms
►
hashing
►
machine_learning
►
math
►
numerical_methods
►
operations_on_datastructures
►
others
►
physics
►
probability
►
range_queries
►
scripts
►
search
▼
sorting
bead_sort.cpp
►
binary_insertion_sort.cpp
bitonic_sort.cpp
►
bogo_sort.cpp
►
bubble_sort.cpp
bucket_sort.cpp
cocktail_selection_sort.cpp
►
comb_sort.cpp
►
count_inversions.cpp
counting_sort.cpp
counting_sort_string.cpp
►
cycle_sort.cpp
►
dnf_sort.cpp
►
gnome_sort.cpp
►
heap_sort.cpp
►
insertion_sort.cpp
►
insertion_sort_recursive.cpp
library_sort.cpp
►
merge_insertion_sort.cpp
►
merge_sort.cpp
►
non_recursive_merge_sort.cpp
numeric_string_sort.cpp
odd_even_sort.cpp
►
pancake_sort.cpp
►
pigeonhole_sort.cpp
►
quick_sort.cpp
►
quick_sort_3.cpp
►
quick_sort_iterative.cpp
radix_sort.cpp
►
radix_sort2.cpp
►
random_pivot_quick_sort.cpp
►
recursive_bubble_sort.cpp
selection_sort_iterative.cpp
►
selection_sort_recursive.cpp
shell_sort.cpp
►
shell_sort2.cpp
slow_sort.cpp
►
stooge_sort.cpp
►
strand_sort.cpp
swap_sort.cpp
tim_sort.cpp
►
wave_sort.cpp
►
wiggle_sort.cpp
►
strings
►
File Members
►
Examples
•
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Friends
Macros
Modules
Pages
Loading...
Searching...
No Matches
heap_sort.cpp
Go to the documentation of this file.
1
23
#include <algorithm>
24
#include <cassert>
25
#include <iostream>
26
36
template
<
typename
T>
37
void
printArray
(T *arr,
int
sz) {
38
for
(
int
i = 0; i < sz; i++) std::cout << arr[i] <<
" "
;
39
std::cout <<
"\n"
;
40
}
37
void
printArray
(T *arr,
int
sz) {
…
}
41
57
template
<
typename
T>
58
void
heapify(T *arr,
int
n,
int
i) {
59
int
largest = i;
60
int
l
= 2 * i + 1;
61
int
r = 2 * i + 2;
62
63
if
(l < n && arr[l] > arr[largest])
64
largest =
l
;
65
66
if
(r < n && arr[r] > arr[largest])
67
largest = r;
68
69
if
(largest != i) {
70
std::swap(arr[i], arr[largest]);
71
heapify(arr, n, largest);
72
}
73
}
74
83
template
<
typename
T>
84
void
heapSort
(T *arr,
int
n) {
85
for
(
int
i = n - 1; i >= 0; i--) heapify(arr, n, i);
86
87
for
(
int
i = n - 1; i >= 0; i--) {
88
std::swap(arr[0], arr[i]);
89
heapify(arr, i, 0);
90
}
91
}
84
void
heapSort
(T *arr,
int
n) {
…
}
92
99
void
test
() {
100
std::cout <<
"Test 1\n"
;
101
int
arr[] = {-10, 78, -1, -6, 7, 4, 94, 5, 99, 0};
102
int
sz =
sizeof
(arr) /
sizeof
(arr[0]);
// sz - size of array
103
printArray
(arr, sz);
// displaying the array before sorting
104
heapSort
(arr, sz);
// calling heapsort to sort the array
105
printArray
(arr, sz);
// display array after sorting
106
assert(std::is_sorted(arr, arr + sz));
107
std::cout <<
"Test 1 Passed\n========================\n"
;
108
109
std::cout <<
"Test 2\n"
;
110
double
arr2[] = {4.5, -3.6, 7.6, 0, 12.9};
111
sz =
sizeof
(arr2) /
sizeof
(arr2[0]);
112
printArray
(arr2, sz);
113
heapSort
(arr2, sz);
114
printArray
(arr2, sz);
115
assert(std::is_sorted(arr2, arr2 + sz));
116
std::cout <<
"Test 2 passed\n"
;
117
}
99
void
test
() {
…
}
118
120
int
main
() {
121
test
();
122
return
0;
123
}
120
int
main
() {
…
}
numerical_methods::simpson_method::l
double l(double x)
Another test function.
Definition
composite_simpson_rule.cpp:119
heapSort
void heapSort(T *arr, int n)
Definition
heap_sort.cpp:84
printArray
void printArray(T *arr, int sz)
Definition
heap_sort.cpp:37
test
void test()
Definition
heap_sort.cpp:99
main
int main()
Definition
heap_sort.cpp:120
sorting
heap_sort.cpp
Generated by
1.12.0