forked from argonautica/sorting-algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.java
37 lines (29 loc) · 891 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
35
36
37
class SelectionSort {
public static void main(String args[]){
int[] arr1 ={27,18,54,43,55,96,37,2,0,4,5,10,1};
System.out.println("Before Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
selectionSort(arr1);
System.out.println("After Selection Sort");
for(int i:arr1){
System.out.print(i+" ");
}
System.out.println();
}
public static void selectionSort(int[] arr){
for (int i=0; i < arr.length - 1;i ++){
int index = i;
for (int j = i+ 1; j <arr.length;j++){
if (arr[j] < arr[index]){
index = j;
}
}
int smallerNumber = arr[index];
arr[index]= arr[i];
arr[i] = smallerNumber;
}
}
}