-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
46 lines (41 loc) · 887 Bytes
/
Copy pathSelectionSort.java
File metadata and controls
46 lines (41 loc) · 887 Bytes
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
38
39
40
41
42
43
44
45
46
/**
*
* @author KAKANAKOU MIGUEL
*
*/
public final class SelectionSort {
/**
* Use the selection sort method to sort the given array A
*
* @param A Array of Comparable that contains the object to sort
*/
public static void sort(Comparable[] A) {
if (A == null)
return;
int indice;
int len = A.length;
for (int i = 0; i < len - 1; i++) {
indice = minIndice(A, i, len);
swap(A, indice, i);
}
}
private static boolean less(Comparable[] A, int i, int j) {
int cmp = A[i].compareTo(A[j]);
if (cmp < 0)
return true;
return false;
}
private static void swap(Comparable[] A, int i, int j) {
Comparable inter = A[i];
A[i] = A[j];
A[j] = inter;
}
private static int minIndice(Comparable[] A, int lo, int hi) {
int indice = lo;
for (int i = lo + 1; i < hi; i++) {
if (less(A, i, indice))
indice = i;
}
return indice;
}
}