-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathselection-sort.java
36 lines (33 loc) · 898 Bytes
/
selection-sort.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
import java.util.Arrays;
import java.util.Scanner;
public class SelectionSort {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int l = in.nextInt();
int[] arr = new int[l];
int start=0;
for(int i=0;i<l;i++) {
arr[i] = in.nextInt();
}
for(int i=0;i<l;i++){
int last=l-1-i;
int maxi = maximum(arr,start,last);
swap(arr,maxi,last);
}
System.out.println(Arrays.toString(arr));
}
static int maximum(int[] 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 swap(int[] arr,int maxi,int last){
int temp=arr[maxi];
arr[maxi]=arr[last];
arr[last]=temp;
}
}