-
Notifications
You must be signed in to change notification settings - Fork 37
/
SelectionSort.java
34 lines (31 loc) · 962 Bytes
/
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
//Program for Selection Sort using max element
import java.util.Arrays;
public class Main {
public static void main(String[] args) {
int[] arr = {4,5,1,2,3};
selectionSort(arr);
System.out.println(Arrays.toString(arr));
}
static void selectionSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
//find the max ele the swap it with correct index
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;
}
private static int getMaxIndex(int[] arr,int start,int end){
int max = start;
for (int i = 0; i <= end; i++) {
if(arr[max] < arr[i]){
max = i;
}
}
return max;
}
}