forked from singh-anant/HacktoberFest22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelection.java
52 lines (46 loc) · 1.29 KB
/
Selection.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
import java.util.Arrays;
public class Selection {
public static void main(String[] args) {
int arr[] = {5,4,3,2,1};
selection(arr,arr.length,0,0);
System.out.println(Arrays.toString(arr));
}
static void selection(int[] arr){
for (int i = 0; i < arr.length; i++) {
// Find the maximum item in array and swap it with last position
int last = arr.length -i-1;
int maxIndex = getMaxIndex(arr,0,last);
Swap(arr, maxIndex, last);
}
}
static void Swap(int[] arr,int first, int last){
int temp = arr[first];
arr[first] = arr[last];
arr[last] = temp;
}
static int getMaxIndex(int[] arr, int start, int end) {
int max = start;
for(int i = start;i<=end;i++){
if(arr[max]<arr[i]){
max = i;
}
}
return max;
}
// Using Recursion
static void selection(int[] arr,int i, int j, int max){
if(i==0){
return;
}
if(i>j){
if(arr[j]>arr[max]){
selection(arr,i,j+1,j);
}else{
selection(arr, i, j+1, max);
}
}else{
Swap(arr, max, i-1);
selection(arr, i-1, 0, 0);
}
}
}