-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRankSelection.java
More file actions
73 lines (64 loc) · 1.49 KB
/
RankSelection.java
File metadata and controls
73 lines (64 loc) · 1.49 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
package math;
/**
* Find the smallest i numbers from a list of n numbers.
*
* TODO: Correct this.
*
* @author shivam.maharshi
*/
public class RankSelection {
// TODO: Correct this!
public static int[] smallestNumbers(int[] arr, int num) {
int[] s = new int[num];
int pivot = partition(arr, num, 0, arr.length - 1);
for (int i = 0; i < pivot; i++) {
System.out.println(arr[i]);
s[i] = arr[i];
}
return s;
}
static int partition(int[] arr, int num, int low, int high) {
int pivot = (low + high) / 2;
while (low <= pivot && pivot < high) {
if (arr[low] > arr[pivot] && arr[high] < arr[pivot]) {
swap(arr, low, high);
low++;
high--;
}
if (arr[low] < arr[pivot]) {
low++;
}
if (arr[high] > arr[pivot]) {
high--;
}
}
while (low < pivot) {
if (arr[low] > arr[pivot]) {
swap(arr, low, pivot);
}
}
while (high > pivot) {
if (arr[high] < arr[pivot]) {
swap(arr, high, pivot);
}
}
if (pivot == num) {
System.out.println("Partition : " + pivot);
return pivot;
}
if (pivot < num) {
return partition(arr, num - pivot, pivot + 1, high);
} else {
return partition(arr, num, low, pivot - 1);
}
}
static void swap(int[] arr, int a, int b) {
int temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
public static void main(String[] args) {
int[] arr = new int[] { 5, 2, 4, 6, 9, 1 };
RankSelection.smallestNumbers(arr, 5);
}
}