-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSelect.java
More file actions
40 lines (37 loc) · 1.06 KB
/
QuickSelect.java
File metadata and controls
40 lines (37 loc) · 1.06 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
package Sorting;
import Util.Swap;
public class QuickSelect {
public static int quickSelect(int [] nums, int k, int start, int end){
if(start >= end){
return nums[k];
}
int mid = nums[(start + end) / 2];
int left = start;
int right = end;
while(left <= right){
while(left <= right && nums[left] < mid){
left += 1;
}
while(left <= right && nums[right] > mid){
right -= 1;
}
if(left <= right){
Swap.swap(nums, left, right);
left += 1;
right -= 1;
}
}
if(right >= k){
return quickSelect(nums, k, start, right);
}
else if (left <= k){
return quickSelect(nums, k , left, end);
}
return nums[k];
}
public static void main(String[] args) {
int[] nums = {3,2,3,1,2,4,5,5,6};
int k = 4;
System.out.println(quickSelect(nums,nums.length - k, 0, nums.length - 1));
}
}