-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselectionsort.java
30 lines (28 loc) · 939 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
public class selectionsort {
public static void main(String[] args) {
int[] nums = { 5, 4, 2, 7, 89, 4 };
int[] res = Selectionsort(nums);
for (int i = 0; i < res.length; i++) {
System.out.println(res[i]);
}
}
public static int[] Selectionsort(int[] arr) {
// Loop through all array elements
for (int i = 0; i < arr.length - 1; i++) {
// Find the minimum element in the unsorted part
int minIndex = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
// Swap the found minimum element with the element at index i
if (minIndex != i) {
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
}
return arr;
}
}