-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.java
32 lines (27 loc) · 993 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
//Time Complexcity is O(n^2) as there are two nexted loops
//works well with small datasets
//In place algorithm as it doesnt require extra space
public class SelectionSort {
public static void printArray(int arr2[]){
for (int i=0;i<arr2.length;i++){
System.out.print(arr2[i]+" ");
}
System.out.println();
}
public static void main(String args[]){
int arr2[]={8,12,15,9,4,1};
//Selection Sort
for (int i=0;i<arr2.length-1;i++){
int smallest=i;
for (int j=i+1;j<arr2.length;j++){//here as the last element isnt sorted so we will take the array length
if (arr2[j]<arr2[smallest]){// if smallest is greater than jth element then sammlest will be j
smallest=j;
}
}
int temp= arr2[smallest];
arr2[smallest]= arr2[i];
arr2[i]= temp;
}
printArray(arr2);
}
}