-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeapSort.java
More file actions
91 lines (81 loc) · 2.84 KB
/
HeapSort.java
File metadata and controls
91 lines (81 loc) · 2.84 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
public class HeapSort {
public Heap HeapObject;
public HeapSort(int[] array){
HeapObject = new Heap(array);
}
public int GetNextMax(){
return HeapObject.GetMax();
}
}
class Heap {
public int[] HeapArray; // хранит неотрицательные числа-ключи
public Heap(int[] array) {
MakeHeap(array);
}
public void MakeHeap(int[] a) {
HeapArray = new int[a.length];
for (int i = 0; i < HeapArray.length; i++) HeapArray[i] = -1;
for (int i = 0; i < a.length; i++) {
Add(a[i]);
}
}
public int GetMax() {
// вернуть значение корня и перестроить кучу
int max;
if (HeapArray[0] == -1) {
return -1;
} else {
max = HeapArray[0];
HeapArray[0] = HeapArray[HeapArray.length-1];
HeapArray[HeapArray.length-1] = -1;
int i = 0;
boolean greater = false;
while (i < HeapArray.length && !greater) {
if (2 * i + 1 < HeapArray.length) {
int node = Math.max(HeapArray[2 * i + 1], HeapArray[2 * i + 2]);
if (node > HeapArray[i]) {
int br = HeapArray[i];
if (node == HeapArray[2 * i + 1]) {
HeapArray[i] = node;
i = 2 * i + 1;
} else if (node == HeapArray[2 * i + 2]) {
HeapArray[i] = node;
i = 2 * i + 2;
}
HeapArray[i] = br;
}else {
greater = true;
}
}else {
i = HeapArray.length;
}
}
}
return max;
}
public boolean Add(int key) {
// добавляем новый элемент key в кучу и перестраиваем её
if (HeapArray[0] == -1) {
HeapArray[0] = key;
return true;
} else {
for (int i = 1; i < HeapArray.length; i++) {
if (HeapArray[i] == -1) {
HeapArray[i] = key;
for (; i >= 0; ) {
if (HeapArray[i] > HeapArray[(i - 1) / 2]) {
int temp = HeapArray[(i - 1) / 2];
HeapArray[(i - 1) / 2] = HeapArray[i];
HeapArray[i] = temp;
}
if (i == 0) {
return true;
}
i = (i - 1) / 2;
}
}
}
}
return false; // если куча вся заполнена
}
}