Skip to content

Commit 26b9692

Browse files
committed
Quick sort (and add variable delay)
1 parent 0cfea85 commit 26b9692

File tree

7 files changed

+68
-45
lines changed

7 files changed

+68
-45
lines changed

src/main/java/sortVisualiser/SortArray.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
public class SortArray extends JPanel {
1818
public static final int WIN_WIDTH = 1280;
1919
public static final int WIN_HEIGHT = 720;
20-
private static final int BAR_WIDTH = 8;
20+
private static final int BAR_WIDTH = 5;
2121
private static final int NUM_BARS = WIN_WIDTH / BAR_WIDTH;
2222

2323
private int[] array;
@@ -41,7 +41,7 @@ public int getValue(int index) {
4141
return array[index];
4242
}
4343

44-
public void swapUpdate(int firstIndex, int secondIndex) {
44+
public void swap(int firstIndex, int secondIndex, long milliSecDelay) {
4545
int temp = array[firstIndex];
4646
array[firstIndex] = array[secondIndex];
4747
array[secondIndex] = temp;
@@ -50,14 +50,28 @@ public void swapUpdate(int firstIndex, int secondIndex) {
5050
barColours[secondIndex] = 100;
5151

5252
repaint();
53-
sleepFor(millisecondsToNano(15));
53+
sleepFor(millisecondsToNano(milliSecDelay));
5454
}
5555

56-
public void updateSingle(int index, int value) {
56+
public void updateSingle(int index, int value, long millisecondDelay) {
5757
array[index] = value;
5858
barColours[index] = 100;
5959
repaint();
60-
sleepFor(millisecondsToNano(15));
60+
sleepFor(millisecondsToNano(millisecondDelay));
61+
}
62+
63+
public void shuffle() {
64+
Random rng = new Random();
65+
for (int i = 0; i < arraySize(); i++) {
66+
int swapWithIndex = rng.nextInt(arraySize() - 1);
67+
swap(i, swapWithIndex, 5);
68+
}
69+
}
70+
71+
public void highlightArray() {
72+
for (int i = 0; i < arraySize(); i++) {
73+
swap(i, i, 5);
74+
}
6175
}
6276

6377
/**
@@ -87,7 +101,7 @@ public void paintComponent(Graphics g) {
87101

88102
graphics.setColor(Color.white);
89103
for (int x = 0; x < NUM_BARS; x++) {
90-
int height = getValue(x) * 3;
104+
int height = getValue(x) * 2;
91105
int xBegin = x + (BAR_WIDTH - 1) * x;
92106
int yBegin = WIN_HEIGHT - height;
93107

src/main/java/sortVisualiser/SortVisualiser.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import sortVisualiser.algorithms.BubbleSort;
66
import sortVisualiser.algorithms.ISortAlgorithm;
77
import sortVisualiser.algorithms.InsertionSort;
8+
import sortVisualiser.algorithms.QuickSort;
89
import sortVisualiser.algorithms.SelectionSort;
9-
import sortVisualiser.algorithms.Shuffle;
1010
import static util.Sleep.secondsToNano;
1111
import static util.Sleep.sleepFor;
1212

@@ -16,9 +16,7 @@
1616
*/
1717
public class SortVisualiser {
1818
private JFrame window;
19-
private SortArray sortArray;
20-
private Shuffle shuffler = new Shuffle();
21-
19+
private SortArray sortArray;
2220
private ArrayList<ISortAlgorithm> sortQueue;
2321

2422
/**
@@ -34,19 +32,14 @@ public SortVisualiser() {
3432
window.setVisible(true);
3533

3634
sortQueue = new ArrayList<>();
35+
sortQueue.add(new QuickSort());
3736
sortQueue.add(new SelectionSort());
3837
sortQueue.add(new InsertionSort());
3938
sortQueue.add(new BubbleSort());
4039
}
41-
42-
private void highlightArray() {
43-
for (int i = 0; i < sortArray.arraySize(); i++) {
44-
sortArray.swapUpdate(i, i);
45-
}
46-
}
47-
40+
4841
private void shuffleAndWait() {
49-
shuffler.runSort(sortArray);
42+
sortArray.shuffle();
5043
sortArray.resetColours();
5144
sleepFor(secondsToNano(2));
5245
}
@@ -56,8 +49,9 @@ public void run() {
5649
sleepFor(secondsToNano(1));
5750
shuffleAndWait();
5851
algorithm.runSort(sortArray);
52+
System.out.println("SORT DONE");
5953
sortArray.resetColours();
60-
highlightArray();
54+
sortArray.highlightArray();
6155
sortArray.resetColours();
6256
}
6357
}

src/main/java/sortVisualiser/algorithms/BubbleSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void runSort(SortArray array) {
1414
for(int i = 0; i < len - 1; i++) {
1515
for (int j = 0; j < len - i - 1; j++) {
1616
if (array.getValue(j) > array.getValue(j + 1)) {
17-
array.swapUpdate(j, j + 1);
17+
array.swap(j, j + 1, 2);
1818
}
1919
}
2020
}

src/main/java/sortVisualiser/algorithms/InsertionSort.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public void runSort(SortArray array) {
1515
int key = array.getValue(i);
1616
int j = i - 1;
1717
while (j >= 0 && array.getValue(j) > key) {
18-
array.updateSingle(j + 1, array.getValue(j));
18+
array.updateSingle(j + 1, array.getValue(j), 5);
1919
j--;
2020
}
21-
array.updateSingle(j + 1, key);
21+
array.updateSingle(j + 1, key, 5);
2222
}
2323
}
2424

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package sortVisualiser.algorithms;
2+
3+
import sortVisualiser.SortArray;
4+
5+
/**
6+
*
7+
* @author mhops
8+
*/
9+
public class QuickSort implements ISortAlgorithm
10+
{
11+
private int findPivotPoint(SortArray array, int lowIndex, int highIndex) {
12+
int pivotValue = array.getValue(highIndex);
13+
int i = lowIndex - 1;
14+
for (int j = lowIndex; j <= highIndex - 1; j++) {
15+
if (array.getValue(j) <= pivotValue) {
16+
i++;
17+
array.swap(i, j, 5);
18+
}
19+
}
20+
array.swap(i + 1, highIndex, 50);
21+
return i + 1;
22+
}
23+
24+
private void quickSort(SortArray array, int lowIndex, int highIndex) {
25+
if (lowIndex < highIndex) {
26+
int pivotPoint = findPivotPoint(array, lowIndex, highIndex);
27+
quickSort(array, lowIndex, pivotPoint - 1);
28+
quickSort(array, pivotPoint + 1, highIndex);
29+
}
30+
}
31+
32+
@Override
33+
public void runSort(SortArray array) {
34+
quickSort(array, 0, array.arraySize() - 1);
35+
}
36+
37+
}

src/main/java/sortVisualiser/algorithms/SelectionSort.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public void runSort(SortArray array) {
1717
minIndex = j;
1818
}
1919
}
20-
array.swapUpdate(i, minIndex);
20+
array.swap(i, minIndex, 50);
2121
}
2222
}
2323
}

src/main/java/sortVisualiser/algorithms/Shuffle.java

Lines changed: 0 additions & 22 deletions
This file was deleted.

0 commit comments

Comments
 (0)