-
Notifications
You must be signed in to change notification settings - Fork 0
/
izziv2.java
63 lines (58 loc) · 1.53 KB
/
izziv2.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
import java.util.*;
public class izziv2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] tabela = new int[n];
for (int i = 0; i < n; i++) {
tabela[i] = sc.nextInt();
}
HeapSort(tabela, n);
}
public static void HeapSort(int[] t, int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
pogrezni(t, i, n);
}
print(t, n);
int r = n - 1;
while (r > 0) {
int x = t[0];
t[0] = t[r];
t[r] = x;
pogrezni(t, 0, r);
print(t, r);
r--;
}
}
public static void pogrezni(int t[], int i, int r) {
int largest = i;
int l = 2 * i + 1;
int d = 2 * i + 2;
if (l < r && t[l] > t[largest])
largest = l;
if (d < r && t[d] > t[largest])
largest = d;
if (largest != i) {
int x = t[i];
t[i] = t[largest];
t[largest] = x;
pogrezni(t, largest, r);
}
}
public static void print(int[] t, int n) {
int potenca = 1;
int j = 1;
for (int i = 0; i < n; i++) {
if (j == 0) {
System.out.printf("| ");
j = potenca << 1;
potenca = j;
}
System.out.print(t[i]);
if (i < n - 1)
System.out.print(" ");
j--;
}
System.out.println();
}
}