-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
47 lines (39 loc) · 964 Bytes
/
Copy pathHeapSort.java
File metadata and controls
47 lines (39 loc) · 964 Bytes
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
package com.ricky;
public class HeapSort {
public static void heapSort(int[] arr){
int switchValue;
BuildMaxHeap(arr);
for(int i=arr.length;i>1;i--){
switchValue=arr[i-1];
arr[i-1]=arr[0];
arr[0]=switchValue;
MaxHeapMaintance(arr, 1,i-1);
}
}
public static void BuildMaxHeap(int[] arr){
for(int i=arr.length/2;i>0;i--){
MaxHeapMaintance(arr, i,arr.length);
}
}
public static void MaxHeapMaintance(int[] arr,int node,int length){
if(node<1){
return;
}
int leftNode=2*node;
int rightNode=2*node+1;
int maxNode=node;
int switchValue;
if(leftNode<=length&&arr[leftNode-1]>arr[node-1]){
maxNode=leftNode;
}
if(rightNode<=length&&arr[rightNode-1]>arr[maxNode-1]){
maxNode=rightNode;
}
if(maxNode!=node){
switchValue=arr[maxNode-1];
arr[maxNode-1]=arr[node-1];
arr[node-1]=switchValue;
MaxHeapMaintance(arr, maxNode,length);
}
}
}