-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort.java
More file actions
33 lines (30 loc) · 881 Bytes
/
Copy pathselectionsort.java
File metadata and controls
33 lines (30 loc) · 881 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
import java.util.Arrays;
public class selectionsort {
public static void main(String[] args) {
int arr[]={8,9,5,6,7,2,4,3};
selection(arr);
System.out.println(Arrays.toString(arr));
}
static void selection(int arr[]){
for(int i=0;i<arr.length;i++){
//find max term
int last=arr.length-i-1;
int maxindex=getMaxindex(arr,0,last);
swapArray(arr, maxindex, last);
}
}
static void swapArray(int arr[],int first,int second){
int temp=arr[first];
arr[first]=arr[second];
arr[second]=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;
}
}