-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
57 lines (51 loc) · 1.45 KB
/
HeapSort.java
File metadata and controls
57 lines (51 loc) · 1.45 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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sortAlgorithms;
/**
*
* @author Vladimir Aca
*/
public class HeapSort {
static int total;
public static void heapify(Comparable[] array, int i){
int left = i*2;
int right = i*2 +1;
int parent = i;
if(left<= total && array[left].compareTo(array[parent])>=0){
parent = left;
}
if(right <= total && array[right].compareTo(array[parent])>=0){
parent = right;
}
if(parent!= i){
swap(array, i, parent);
heapify(array, parent);
}
}
public static void swap(Comparable[] array, int i, int parent){
Comparable temp = array[i];
array[i] = array[parent];
array[parent] = temp;
}
public static void sort(Comparable[] array){
total = array.length-1;
for(int i=total/2; i>=0; i--){
heapify(array, i);
}
print((Integer[]) array);
for(int i=total; i>0; i--){
swap(array, 0, i);
total--;
heapify(array, 0);
}
}
public static void print(Integer[] currentArray){
for(Integer element: currentArray){
System.out.print(element + " ");
}
System.out.println("\n");
}
}