-
Notifications
You must be signed in to change notification settings - Fork 0
/
Selectionsort.java
55 lines (49 loc) · 1.48 KB
/
Selectionsort.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
import java.util.*;
public class Selectionsort {
public static void main(String[] args){
Integer arr [] = {7,4,9,5,2,1,6,0,8,3};
selection(arr,arr.length,0,0);
System.out.println(Arrays.toString(arr));
}
// static void selection(Integer[] arr) {
// for(int i =0 ; i < arr.length ; i++) {
// int last = arr.length - i - 1;
// //List<Integer> list = Arrays.asList(arr);
// //int max = Collections.max(list);
// int Max_ind =maxi(arr,0,last);
// swap(arr,Max_ind,last);
// }
// }
public static void swap(Integer[] arr, int max, int last) {
int temp = arr[last];
arr[last] = arr[max];
arr[max] = temp;
}
private static int maxi(Integer[] arr, int start, int end){
int max = start;
for(int i = start ; i < end ; i++) {
if(arr[i]>=arr[max]){
max = i;
}
}
return max;
}
static void selection(Integer[] arr,int r,int c,int max){
if(r==0)
return;
else{
if(c<r){
if (arr[c]>arr[max]){
selection(arr,r,c+1,c);
}else{
selection(arr,r,c+1,max);
}
}else {
int temp = arr[max];
arr[max] = arr[r-1];
arr[r-1] = temp;
selection(arr,r-1,0,0);
}
}
}
}