Skip to content

Commit 98042d3

Browse files
committed
Make the timing between algorithms better
1 parent 6fe4691 commit 98042d3

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

src/main/java/sortVisualiser/SortArray.java

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.awt.Graphics2D;
77
import java.util.Random;
88
import javax.swing.JPanel;
9+
import static util.Sleep.microsecondsToNano;
910
import static util.Sleep.sleepFor;
1011

1112
/**
@@ -17,16 +18,17 @@ public class SortArray extends JPanel {
1718
public static final int WIN_HEIGHT = 720;
1819
private static final int BAR_WIDTH = 8;
1920
private static final int NUM_BARS = WIN_WIDTH / BAR_WIDTH;
21+
2022
private int[] array;
21-
private int[] accessColourMod;
23+
private int[] barColours;
2224

2325
public SortArray() {
2426
setBackground(Color.darkGray);
2527
array = new int[NUM_BARS];
26-
accessColourMod = new int[NUM_BARS];
28+
barColours = new int[NUM_BARS];
2729
for (int i = 0; i < NUM_BARS; i++) {
2830
array[i] = i;
29-
accessColourMod[i] = 0;
31+
barColours[i] = 0;
3032
}
3133
}
3234

@@ -43,10 +45,11 @@ public void swapUpdate(int firstIndex, int secondIndex) {
4345
array[firstIndex] = array[secondIndex];
4446
array[secondIndex] = temp;
4547

46-
accessColourMod[firstIndex] = 100;
47-
accessColourMod[secondIndex] = 100;
48+
barColours[firstIndex] = 100;
49+
barColours[secondIndex] = 100;
50+
4851
repaint();
49-
sleepFor(10000);
52+
sleepFor(microsecondsToNano(10000));
5053
}
5154

5255
/**
@@ -60,7 +63,7 @@ public Dimension getPreferredSize() {
6063

6164
public void resetColours() {
6265
for (int i = 0; i < NUM_BARS; i++) {
63-
accessColourMod[i] = 0;
66+
barColours[i] = 0;
6467
}
6568
repaint();
6669
}
@@ -80,11 +83,11 @@ public void paintComponent(Graphics g) {
8083
int xBegin = x + (BAR_WIDTH - 1) * x;
8184
int yBegin = WIN_HEIGHT - height;
8285

83-
int val = accessColourMod[x] * 2;
86+
int val = barColours[x] * 2;
8487
graphics.setColor(new Color(255, 255 - val, 255 - val));
8588
graphics.fillRect(xBegin, yBegin, BAR_WIDTH, height);
86-
if (accessColourMod[x] > 0) {
87-
accessColourMod[x]-= 5;
89+
if (barColours[x] > 0) {
90+
barColours[x]-= 20;
8891
}
8992
}
9093
}

src/main/java/sortVisualiser/SortVisualiser.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import sortVisualiser.algorithms.BubbleSort;
66
import sortVisualiser.algorithms.ISortAlgorithm;
77
import sortVisualiser.algorithms.Shuffle;
8+
import static util.Sleep.secondsToNano;
89
import static util.Sleep.sleepFor;
910

1011
/**
@@ -14,6 +15,7 @@
1415
public class SortVisualiser {
1516
private JFrame window;
1617
private SortArray sortArray;
18+
private Shuffle shuffler = new Shuffle();
1719

1820
private ArrayList<ISortAlgorithm> sortQueue;
1921

@@ -30,15 +32,29 @@ public SortVisualiser() {
3032
window.setVisible(true);
3133

3234
sortQueue = new ArrayList<>();
33-
sortQueue.add(new Shuffle());
3435
sortQueue.add(new BubbleSort());
3536
}
3637

38+
private void highlightArray() {
39+
for (int i = 0; i < sortArray.arraySize(); i++) {
40+
sortArray.swapUpdate(i, i);
41+
}
42+
}
43+
44+
private void shuffleAndWait() {
45+
shuffler.runSort(sortArray);
46+
sortArray.resetColours();
47+
sleepFor(secondsToNano(2));
48+
}
49+
3750
public void run() {
3851
for (ISortAlgorithm algorithm : sortQueue) {
52+
sleepFor(secondsToNano(2));
53+
shuffleAndWait();
3954
algorithm.runSort(sortArray);
4055
sortArray.resetColours();
41-
sleepFor(2000000);
56+
highlightArray();
57+
sortArray.resetColours();
4258
}
4359
}
4460

src/main/java/util/Sleep.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,23 @@
55
* @author mhops
66
*/
77
public class Sleep {
8-
public static void sleepFor(int microseconds) {
9-
long nanoseconds = microseconds * 1000;
8+
public static void sleepFor(long nanoseconds) {
109
long timeElapsed;
1110
final long startTime = System.nanoTime();
1211
do {
1312
timeElapsed = System.nanoTime() - startTime;
1413
} while(timeElapsed < nanoseconds);
1514
}
15+
16+
public static long secondsToNano(long time) {
17+
return time * (long)Math.pow(10, 9);
18+
}
19+
20+
public static long millisecondsToNano(long time) {
21+
return time * (long)Math.pow(10, 6);
22+
}
23+
24+
public static long microsecondsToNano(long time) {
25+
return time * (long)Math.pow(10, 3);
26+
}
1627
}

0 commit comments

Comments
 (0)