forked from iiitv/algos
-
Notifications
You must be signed in to change notification settings - Fork 1
/
heapsort.cpp
58 lines (50 loc) · 1.57 KB
/
heapsort.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#include <bits/stdc++.h>
using namespace std;
void swap(int array[], int index1, int index2) //swapping function wihout using third variable
{
array[index1] = array[index1] + array[index2];
array[index2] = array[index1] - array[index2];
array[index1] = array[index1] - array[index2];
}
void heapify(int array[], int size, int index)
{
int largest = index; //considering largest as root
int left = 2 * index + 1;
int right = 2 * index + 2;
if (left < size && array[left] > array[largest]) //left child is larger then root
largest = left;
if (right < size && array[right] > array[largest]) //right child is larger then root
largest = right;
if (largest != index) //condition if largest is not root
{
swap(array, index, largest);
heapify(array, size, largest);
}
}
void heapsort(int array[], int size)
{
for (int i = size / 2 - 1; i >= 0; i--) //Create heap from array rearrangement
heapify(array, size, i);
for (int i = size - 1; i > 0; i--) //move current root to end a step for Extract min process
{
swap(array, 0, i); //swap element
heapify(array, i, 0); //excecuting heapify to bring all elements in correct position again
}
}
void display(int array[], int size)
{
int i = 0;
for (i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
int main()
{
int array[] = {6, 3, 6, 343, 42, 1, 43, 35, 34, 43};
int size = sizeof(array) / sizeof(array[0]);
cout << "Unsorted array is :" << endl;
display(array, size); //display the unsorted array
heapsort(array, size);
cout << "sorted array is :" << endl;
display(array, size); //display the sorted array
}