-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheapSort.java
More file actions
54 lines (47 loc) · 1.54 KB
/
heapSort.java
File metadata and controls
54 lines (47 loc) · 1.54 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
class Solution
{
//Function to build a Heap from array.
void buildHeap(int arr[], int n)
{
//calling heapify function for values from half to first index.
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
}
//Heapify function to maintain heap property.
void heapify(int arr[], int n, int i)
{
int largest = i;
int leftChild = 2*i + 1;
int rightChild = 2*i + 2;
//if left or right child is greater than current element,
//we store its position.
if (leftChild < n && arr[leftChild] > arr[largest])
largest = leftChild;
if (rightChild < n && arr[rightChild] > arr[largest])
largest = rightChild;
//if largest is not equal to i, we swap the values at their position.
if (largest != i)
{
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
//calling function recursively for the largest variable.
heapify(arr, n, largest);
}
}
//Function to sort an array using Heap Sort.
public void heapSort(int arr[], int n)
{
//calling function to build heap with array.
buildHeap(arr, n);
for (int i=n-1; i>=0; i--)
{
//swapping values at current and first index.
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
//calling heapify function for first index.
heapify(arr, i, 0);
}
}
}