-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelection2.java
More file actions
97 lines (85 loc) · 1.64 KB
/
Selection2.java
File metadata and controls
97 lines (85 loc) · 1.64 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
package Algorithm.selection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Random;
public class Selection2 {
int comparison=0;
int array[];
public Selection2(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 pivotIndex=new Random().nextInt(right-left)+left;
int pivot=arr[pivotIndex];
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 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;
}
}