-
Notifications
You must be signed in to change notification settings - Fork 0
/
SelectionSort.java
33 lines (28 loc) · 1002 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
import java.util.Arrays;
public class SelectionSort {
private static Comparable[] array = {2,8,4,7,1,5,9,3,6};
public static void main(String[] args) {
SelectionSort sSort = new SelectionSort();
sSort.sort(array);
System.out.println(Arrays.toString(array));
}
public void sort(Comparable[] a) {
int n = a.length;
for (int i = 0; i < n; i++) {
int minIndex = i;
for (int j = i + 1; j < n; j++)
if (a[j].compareTo(a[minIndex]) < 0){
minIndex = j;
}
if (minIndex != i){
Comparable temp = a[i];
a[i] = a[minIndex];
a[minIndex] = temp;
} //! Remove comments or go to the readMe to understand the algorithms better
//for(int k =0; k<array.length;k++){
// System.out.print(array[k] + ", ");
//}
//System.out.println();
}
}
}