-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathSelectionSort.java
More file actions
44 lines (40 loc) · 1.17 KB
/
Copy pathSelectionSort.java
File metadata and controls
44 lines (40 loc) · 1.17 KB
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
package algorithms.sorting;
import java.util.Arrays;
/**
* created by Cenk Canarslan on 2021-01-31
*/
public class SelectionSort {
/**
* Time Complexity :
* - Worst : O(n^2)
* - Average : O(n^2)
* - Best : O(n^2)
*
* Space Complexity :
* O(1)
*
* @param arr
*/
private void sort(int[] arr) {
System.out.println("initial:" + Arrays.toString(arr));
for (int i = 0; i < arr.length; i++) {
int minPos = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minPos]) {
minPos = j;
}
System.out.println("(i=" + i + ",j=" + j + ")-[" + arr[j] + ":" + arr[minPos] + "]");
}
int tmp = arr[minPos];
arr[minPos] = arr[i];
arr[i] = tmp;
System.out.println(Arrays.toString(arr));
}
}
public static void main(String[] args) {
int[] arr = new int[]{64, 34, 25, 12, 22, 11, 90, 12};
SelectionSort selectionSort = new SelectionSort();
selectionSort.sort(arr);
System.out.println("sorted:" + Arrays.toString(arr));
}
}