-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise13_03.java
More file actions
37 lines (32 loc) · 1 KB
/
Exercise13_03.java
File metadata and controls
37 lines (32 loc) · 1 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
import java.util.ArrayList;
public class Exercise13_03 {
public static void main(String[] args) {
ArrayList<Number> list = new ArrayList<Number>();
list.add(14);
list.add(24);
list.add(4);
list.add(42);
list.add(5);
sort(list);
for (int i = 0; i < list.size(); i++)
System.out.print(list.get(i) + " ");
}
public static void sort(ArrayList<Number> list) {
for (int i = 0; i < list.size() - 1; i++) {
// Find the minimum in the list[i..list.length-1]
Number currentMin = list.get(i);
int currentMinIndex = i;
for (int j = i + 1; j < list.size(); j++) {
if (currentMin.doubleValue() > list.get(j).doubleValue()) {
currentMin = list.get(j);
currentMinIndex = j;
}
}
// Swap list.get(i) with list.get(currentMinIndex) if necessary;
if (currentMinIndex != i) {
list.set(currentMinIndex, list.get(i));
list.set(i, currentMin);
}
}
}
}