forked from WWaldo/Sorting-Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBubbleSort.java
54 lines (48 loc) · 1.39 KB
/
BubbleSort.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;
//BubbleSort
//Runs through data, swapping 2 neighboring data points, and continuing down the list, returning to the beginning once at the end until it is sorted
public class BubbleSort extends Sorts{
public ArrayList<Integer> bubbleUp(ArrayList<Integer> unsortedList)
{
ArrayList<Integer> sortedList = new ArrayList<Integer>();
boolean swapped = true;
while(swapped)
{
swapped = false;
for(int i = 1; i < unsortedList.size(); i++)
{
if(unsortedList.get(i) < unsortedList.get(i - 1))
{
//Swapping neighboring data points
int holder = unsortedList.get(i);
unsortedList.set(i, unsortedList.get(i - 1));
unsortedList.set(i - 1, holder);
swapped = true;
}
}
}
sortedList = unsortedList;
return sortedList;
}
public void BubbleTime(IOClass ioStream) throws IOException
{
ioStream.readFromFile();
ArrayList<Integer> sortedList = new ArrayList<Integer>();
long timeBefore = System.nanoTime();
sortedList = bubbleUp(ioStream.getInputArray());
long timeAfter = System.nanoTime();
double rawTime = timeAfter - timeBefore;
double timeInMilli = rawTime/1000000;
if(isSorted(sortedList))
{
ioStream.setInputArray(sortedList);
System.out.print("BubbleSort time (in Milli): ");
System.out.println(timeInMilli);
}
else
{
System.out.println("Not sorted!");
}
}
}