-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSelectionSort.java
More file actions
32 lines (28 loc) · 883 Bytes
/
Copy pathSelectionSort.java
File metadata and controls
32 lines (28 loc) · 883 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
import java.util.ArrayList;
public class SelectionSort {
public static ArrayList<Integer> selection_sort(ArrayList<Integer> sort) {
int length = sort.size();
for (int i = 0; i < length; i++) {
int minvalue = i;
for (int j = i + 1; j < length; j++) {
if(sort.get(j) < sort.get(minvalue)){
minvalue = j;
}
}
if (minvalue != i) {
swap(sort, minvalue, i);
}
}
return sort;
}
static void swap(ArrayList<Integer> arr, int i, int j){
int temp = arr.get(i);
arr.set(i, arr.get(j));
arr.set(j, temp);
}
public static void print(ArrayList<Integer> sort){
for (Integer integer : sort) {
System.out.println(integer);
}
}
}