forked from WWaldo/Sorting-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSelectionSort.java
54 lines (47 loc) · 1.22 KB
/
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
import java.io.IOException;
import java.util.ArrayList;
public class SelectionSort extends Sorts{
public ArrayList<Integer> selectionSort(ArrayList<Integer> unsortedList)
{
//Runs through the list
for(int i = 0; i < unsortedList.size(); i++)
{
//moves smallest number to index i
int index = i;
for(int j = i + 1; j < unsortedList.size(); j++)
{
if(unsortedList.get(index) > unsortedList.get(j))
{
index = j;
}
}
if(i != index)
{
int holder = unsortedList.get(index);
unsortedList.set(index, unsortedList.get(i));
unsortedList.set(i, holder);
}
}
return unsortedList;
}
public void SelectionTime(IOClass ioStream) throws IOException
{
ioStream.readFromFile();
ArrayList<Integer> sortedList = new ArrayList<Integer>();
long timeBefore = System.nanoTime();
sortedList = selectionSort(ioStream.getInputArray());
long timeAfter = System.nanoTime();
double rawTime = timeAfter - timeBefore;
double timeInMilli = rawTime/1000000;
if(isSorted(sortedList))
{
ioStream.setInputArray(sortedList);
System.out.print("SelectionSort time (in Milli): ");
System.out.println(timeInMilli);
}
else
{
System.out.println("Not sorted!");
}
}
}