forked from frigid-lynx/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSortAlgorithmEx.java
33 lines (31 loc) · 1015 Bytes
/
SelectionSortAlgorithmEx.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
public class SelectionSortAlgorithmEx {
public static void main(String[] args){
System.out.println("Selection sort algorithm");
System.out.println("------------------------");
int[] array = {5,12,4,6,9,8,3,7};
System.out.println("Elements of the array : ");
for(int i:array){
System.out.print(i+" ");
}
System.out.println();
selectionSort(array);
System.out.println("After the Selection Sort : ");
for(int i:array){
System.out.print(i+" ");
}
}
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 smallerNo = arr[index];
arr[index] = arr[i];
arr[i] = smallerNo;
}
}
}