Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Latest commit

 

History

History
History
39 lines (36 loc) · 1.03 KB

File metadata and controls

39 lines (36 loc) · 1.03 KB
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
import java.util.*;
public class Sort {
/**
* @param list
* @return
*/
static ArrayList<Integer> selectionSort(ArrayList<Integer> list) {
ArrayList<Integer> sortedList = new ArrayList<Integer>();
int length = list.size();
for(int i = 0; i < length; i++) {
int indexToMove = indexOfMin(list);
final int min = list.get(indexToMove);
list.remove(indexToMove);
sortedList.add(min);
}
return sortedList;
}
/**
* @param list
* @return
*/
static int indexOfMin(ArrayList<Integer> list) {
int minIndex = 0;
int length = list.size();
for (int i = 0; i < length; i++) {
if(list.get(i) < list.get(minIndex)) {
minIndex = i;
}
}
return minIndex;
}
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<Integer>(Arrays.asList(29, 100, 1, 2, 57));
System.out.println(selectionSort(list));
}
}
Morty Proxy This is a proxified and sanitized view of the page, visit original site.