forked from gaurav03kr/hello-hacktoberfest-2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Heap_sort_array.java
74 lines (59 loc) · 1.48 KB
/
Heap_sort_array.java
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
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package com;
/*
*
* @author zenalarifin
*/
import java.util.Arrays;
public class HeapSort {
public void sort(int arr[])
{
int n = arr.length;
// Build heap
for (int i = n / 2 - 1; i >= 0; i--)
heapify(arr, n, i);
// Extract element from heap
for (int i = n - 1; i > 0; i--) {
// Move root
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// Max heapify reduced heap
heapify(arr, i, 0);
}
}
// To heapify a subtree that has root node i
void heapify(int arr[], int n, int i)
{
int large = i; // Set largest as root
int left = 2 * i + 1;
int right = 2 * i + 2;
// If left child is larger than root
if (left < n && arr[left] > arr[large])
large = left;
// If right child is larger than largest so far
if (right< n && arr[right] > arr[large])
large = right;
// If largest is not root
if (large != i) {
int swap = arr[i];
arr[i] = arr[large];
arr[large] = swap;
// Recursively heapify the subtree
heapify(arr, n, large);
}
}
// Main driver code
public static void main(String args[])
{
int arr[] = { 132, 44, 5, 42, 45, 167, 4, 52, 31, 16, 42 };
int n = arr.length;
HeapSort heap = new HeapSort();
heap.sort(arr);
System.out.println("HeapSorted array is:");
System.out.println(Arrays.toString(arr));
}
}