File tree Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Expand file tree Collapse file tree 1 file changed +26
-0
lines changed Original file line number Diff line number Diff line change
1
+ /**
2
+ * Utility class for sorting an array using Selection Sort algorithm. Selection
3
+ * Sort is a basic algorithm for sorting with O(n^2) time complexity. Basic idea
4
+ * of this algorithm is to find a local minimum, which is the minimum value from
5
+ * (i+1) to length of the array [i+1, arr.length), and swap it with the current
6
+ * working index (i).
7
+ *
8
+ * @author Kongpon Charanwattanakit
9
+ *
10
+ */
1
11
public class SelectionSort {
2
12
13
+ /**
14
+ * Sort an array using Selection Sort algorithm.
15
+ *
16
+ * @param arr
17
+ * is an array to be sorted
18
+ */
3
19
public static void sort (int [] arr ) {
4
20
for (int i = 0 ; i < arr .length ; i ++) {
5
21
int min = i ;
@@ -13,6 +29,16 @@ public static void sort(int[] arr) {
13
29
}
14
30
}
15
31
32
+ /**
33
+ * Utility method for swapping elements in an array.
34
+ *
35
+ * @param arr
36
+ * is an array to be swapped
37
+ * @param i
38
+ * is index of first element
39
+ * @param j
40
+ * is index of second element
41
+ */
16
42
private static void swap (int [] arr , int i , int j ) {
17
43
int temp = arr [i ];
18
44
arr [i ] = arr [j ];
You can’t perform that action at this time.
0 commit comments