-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLab8_heap.cpp
More file actions
86 lines (68 loc) · 1.9 KB
/
Lab8_heap.cpp
File metadata and controls
86 lines (68 loc) · 1.9 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// function to heapify the subtree rooted at node- root
void heapify(int arr[], int n, int root)
{
int Greatest = root; // initialize biggest as the root because 0 is the starting index
int Left = 2*root + 1; // left child of node
int Right = 2*root + 2; // right child of node
// if left child greater than root node
if (Left < n && arr[Left] > arr[Greatest])
Greatest = Left;
// if right child greater than everything till now
if (Right < n && arr[Right] > arr[Greatest])
Greatest = Right;
// if greatest isnt root
if (Greatest != root)
{
swap(arr[root], arr[Greatest]);
// recursively heapify sub-tree
heapify(arr, n, Greatest);
}
}
// function to implement heap sort
void heapSort(int arr[], int n)
{
// build heap by rearranging the array
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// extracting elements from heap one by one
for (int i = n - 1; i >= 0; i--)
{
// move current root to the end
swap(arr[0], arr[i]);
// now calling max heapify for reduced heap
heapify(arr, i, 0);
}
}
// print contents of array
void displayArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
cout << arr[i] << " ";
cout << "\n";
}
// main program
int main()
{
int n;
// get user input for array size preffered
cout << "Enter size of array: ";
cin >> n;
//create heap array of size n
int heap_arr[n];
// get user input for array values
cout << "Enter array values:" << endl;
for (int i = 0; i < n; i++) {
cout << "arr[" << i << "]: ";
cin >> heap_arr[i];
}
cout << "Input array:" << endl;
displayArray(heap_arr, n);
heapSort(heap_arr, n);
cout << "Sorted array:" << endl;
displayArray(heap_arr, n);
return 0;
}