-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathHeap_Sort.cpp
56 lines (49 loc) · 1.17 KB
/
Heap_Sort.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
class Solution
{
public:
//Heapify function to maintain heap property.
void heapify(int arr[], int n, int i)
{
// Your Code Here 0 BASED INDEXING
int largest=i;
int left=2*i+1;
int right=2*i+2;
if(left<n && arr[largest]<arr[left])
{
largest=left;
}
if(right<n && arr[largest]<arr[right])
{
largest=right;
}
if(largest!=i)
{
swap(arr[largest], arr[i]);
heapify(arr, n, largest);
}
}
public:
//Function to build a Heap from array.
void buildHeap(int arr[], int n)
{
// Your Code Here
for(int i=n/2;i>=0;i--)
{
heapify(arr, n, i);
}
}
public:
//Function to sort an array using Heap Sort.
void heapSort(int arr[], int n)
{
//code here
buildHeap(arr, n);
for(int i=n-1;i>=0;i--)
{
// step-1: swap last and root node
swap(arr[0], arr[i]);
// step-2: sahi positionpe lao using heapify
heapify(arr, i, 0);
}
}
};