Skip to content

Commit 6818098

Browse files
Update SelectionSort.java
-Used compareTo -Used a local swap method
1 parent 19caa97 commit 6818098

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

Sorts/SelectionSort.java

+13-2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@
88

99
public class SelectionSort implements SortAlgorithm {
1010

11+
/**
12+
* This method swaps the two elements in the array
13+
* @param arr, i, j The array for the swap and
14+
the indexes of the to-swap elements
15+
*/
16+
public void swap(T[] arr, int i, int j) {
17+
T temp = arr[i];
18+
arr[i] = arr[j];
19+
arr[j] = temp;
20+
}
21+
1122
/**
1223
* This method implements the Generic Selection Sort
1324
*
@@ -22,14 +33,14 @@ public <T extends Comparable<T>> T[] sort(T[] arr) {
2233
int min = i;
2334

2435
for (int j = i + 1; j < n; j++) {
25-
if (SortUtils.less(arr[j], arr[min])) {
36+
if (arr[min].compareTo(arr[j]) < 0) {
2637
min = j;
2738
}
2839
}
2940

3041
// Swapping if index of min is changed
3142
if (min != i) {
32-
SortUtils.swap(arr, i, min);
43+
swap(arr, i, min);
3344
}
3445
}
3546

0 commit comments

Comments
 (0)