-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCS263_Lab_0.java
94 lines (83 loc) · 2.13 KB
/
CS263_Lab_0.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
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
92
93
94
// Question: Find the kth largest number in best possible time complexity.
import java.util.*;
public class kth_largest_heapmethod {
public static void swap(int a[], int j, int k) {
int temp = a[j];
a[j] = a[k];
a[k] = temp;
}
public static void printArray(int a[], int n) {
for (int i = 0; i < n; i++) System.out.print(a[i] + " ");
System.out.println();
}
public static void heapify(int a[], int n, int i) {
int largest = i;
int l = 2 * i;
int r = 2 * i + 1;
if (l <= n && a[l] > a[largest]) largest = l;
if (r <= n && a[r] > a[largest]) largest = r;
if (largest != i) {
swap(a, i, largest);
heapify(a, n, largest);
}
}
public static void minheapify(int a[], int n, int i) {
int smallest = i;
int l = 2 * i;
int r = 2 * i + 1;
if (l <= n && a[l] < a[smallest]) smallest = l;
if (r <= n && a[r] < a[smallest]) smallest = r;
if (smallest != i) {
swap(a, i, smallest);
minheapify(a, n, smallest);
}
}
public static void buildheap(int a[], int n) {
for (int i = n / 2; i > 0; i--) {
heapify(a, n, i);
}
}
public static void buildminheap(int a[], int n) {
for (int i = n / 2; i > 0; i--) {
minheapify(a, n, i);
}
}
public static int deleterootnode(int a[], int n) {
int last = a[n - 1];
a[0] = last;
n--;
heapify(a, n, 0);
return n;
}
public static int deleteminrootnode(int a[], int n) {
int last = a[n - 1];
a[0] = last;
n--;
minheapify(a, n, 0);
return n;
}
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int a[] = { 0, 3, 6, 4, 1, 2, 7, 8, 9 };
int n = a.length - 1;
System.out.print("The present array: ");
printArray(a, n);
int num = n;
int k = 7;
if (k <= n / 2) {
buildheap(a, n);
while (k-- > 0) {
n = deleterootnode(a, n);
}
} else {
buildminheap(a, n);
int s = n - k + 1;
while (s-- > 0) {
n = deleteminrootnode(a, n);
}
System.out.println(
"The kth largest element where k = " + k + " is: " + a[0]
);
}
}
}