-
Notifications
You must be signed in to change notification settings - Fork 80
Expand file tree
/
Copy pathSelection_Sort.java
More file actions
35 lines (29 loc) · 825 Bytes
/
Selection_Sort.java
File metadata and controls
35 lines (29 loc) · 825 Bytes
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
import java.util.Arrays;
public class Selection {
public static void main(String[] args) {
int[] arr={1,3,6,2,9};
selection(arr);
System.out.println(Arrays.toString(arr));
}
static void selection(int[] arr){
for(int i=0;i<arr.length;i++){
int last=arr.length-i-1;
int maxIndex=findMaxIndex(arr,0,last);
swap(arr,maxIndex,last);
}
}
static void swap(int[] arr,int maxIndex,int last){
int temp=arr[maxIndex];
arr[maxIndex]=arr[last];
arr[last]=temp;
}
static int findMaxIndex(int[] array, int start, int end){
int max=start;
for(int i=start+1;i<=end;i++){
if(array[i]>array[max]){
max=i;
}
}
return max;
}
}