-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSem5HomeWork3.java
More file actions
73 lines (60 loc) · 1.32 KB
/
Copy pathSem5HomeWork3.java
File metadata and controls
73 lines (60 loc) · 1.32 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
//Реализовать алгоритм пирамидальной сортировки (HeapSort).
class Sem5HomeWork3 {
public static int LEFT(int i) {
return (2*i + 1);
}
public static int RIGHT(int i) {
return (2*i + 2);
}
public static void swap(int[] arr, int i, int j)
{
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public static void heapify(int[] arr, int i, int size)
{
int left = LEFT(i);
int right = RIGHT(i);
int largest = i;
if (left < size && arr[left] > arr[i]) {
largest = left;
}
if (right < size && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i)
{
swap(arr, i, largest);
heapify(arr, largest, size);
}
}
public static int pop(int[] arr, int size)
{
if (size <= 0) {
return -1;
}
int top = arr[0];
arr[0] = arr[size-1];
heapify(arr, 0, size - 1);
return top;
}
public static void heapsort(int[] arr)
{
int n = arr.length;
int i = (n - 2) / 2;
while (i >= 0) {
heapify(arr, i--, n);
}
while (n > 0)
{
arr[n - 1] = pop(arr, n);
n--;
}
}
public static void main(String[] args) {
int[] listNum = new int [] {5, 2, 3, 8, 10, 5, -1};
heapsort(listNum);
for(int el: listNum) System.out.printf("%d ", el);
}
}