-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection3.java
More file actions
117 lines (102 loc) · 2.89 KB
/
Selection3.java
File metadata and controls
117 lines (102 loc) · 2.89 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
package Algorithm.selection;
public class Selection3 {
int comparison = 0;
int array[];
public Selection3(int[] arr) {
this.array = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
this.array[i] = arr[i];
}
}
public int select(int k) {
if (array.length == 1) {
return array[0];
}
if (array.length < 25) {
insertionSort(array);
return array[k - 1];
}
return quickSelect(array, k, 0, array.length - 1);
}
private int quickSelect(int[] arr, int k, int left, int right) {
if (left >= right) {
return arr[left];
}
int pivot = findPivot(arr, left, right);
int l = left;
int g = right;
int i = left;
while (i <= g) {
int comp = compareTo(arr[i], pivot);
if (comp < 0) {
swap(arr, l++, i++);
} else if (comp == 0) {
++i;
} else {
swap(arr, i, g--);
}
}
if (k <= l - left) {
return quickSelect(arr, k, left, l - 1);
} else if (k <= g + 1 - left) {
return arr[l];
} else {
return quickSelect(arr, k + left - g - 1, g + 1, right);
}
}
private int findPivot(int[] arr, int left, int right) {
if (right - left <= 4) {
insertionSort(arr, left, right);
return arr[(left + right) / 2];
}
int cursor = left - 1;
for (int i = left; i <= right; i += 5) {
int end = i + 4 <= right ? i + 4 : right;
insertionSort(arr, i, end);
int median = (i + end) / 2;
swap(arr, ++cursor, median);
}
return findPivot(arr, left, cursor);
}
private void insertionSort(int[] arr, int left, int right) {
int temp;
for (int i = 1 + left; i <= right; ++i) {
temp = arr[i];
int j = i - 1;
for (; j >= left && compare(temp, arr[j]); j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
private void insertionSort(int[] arr) {
int temp;
for (int i = 1; i < arr.length; ++i) {
temp = arr[i];
int j = i - 1;
for (; j >= 0 && compare(temp, arr[j]); j--) {
arr[j + 1] = arr[j];
}
arr[j + 1] = temp;
}
}
public static void swap(int[] arr, int i, int j) {
int temp = 0;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
public boolean compare(int a, int b) {
comparison++;
return a < b;
}
public int compareTo(int a, int b) {
++comparison;
if (a < b)
return -1;
else if (a > b)
return 1;
else
return 0;
}
}