-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionProcedure.java
57 lines (47 loc) · 1.64 KB
/
selectionProcedure.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
import java.util.*;
public class selectionProcedure {
// function to do the partition of an array
public static int partition(int[] arr, int l, int h){
// first element as the pivot element in an array
int pivot = arr[l];
int i = l;
for(int j=l+1; j<=h; j++){
if(arr[j] <= pivot){
i = i + 1;
// swap(arr[i], arr[j])
int tem = arr[i];
arr[i] = arr[j];
arr[j] = tem;
}
}
// swap (arr[l], arr[i])
// to get the correct position of the pivot element
int tem = arr[l];
arr[l] = arr[i];
arr[i] = tem;
// index of the pivot element
return i;
}
public static int selectionProcedure(int[] arr, int l, int h, int k){
//1. Dividing the problem into subproblems
int m = partition(arr, l, h);
// relationship between the index number and the position number
if(m == k-1){
return arr[m];
}
else if(m < k-1){
// traverse towards right side of an array
// 2. Conquer the subproblems via recursion
return selectionProcedure(arr, m+1, h, k);
}
// traverse towards left side of an array
else return selectionProcedure(arr, l, m-1, k);
}
public static void main(String[] args){
int[] arr = {50, 30, 70, 90, 10, 34, 89, 98, 13};
int n = arr.length;
int k = 6;
int result = selectionProcedure(arr, 0, n-1, k);
System.out.println("The kth smallest element in an array is: "+result);
}
}