Skip to content

Commit 0cfea85

Browse files
committed
Add selection sort and insert sort
1 parent 98042d3 commit 0cfea85

File tree

4 files changed

+62
-2
lines changed

4 files changed

+62
-2
lines changed

src/main/java/sortVisualiser/SortArray.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.Random;
88
import javax.swing.JPanel;
99
import static util.Sleep.microsecondsToNano;
10+
import static util.Sleep.millisecondsToNano;
1011
import static util.Sleep.sleepFor;
1112

1213
/**
@@ -49,7 +50,14 @@ public void swapUpdate(int firstIndex, int secondIndex) {
4950
barColours[secondIndex] = 100;
5051

5152
repaint();
52-
sleepFor(microsecondsToNano(10000));
53+
sleepFor(millisecondsToNano(15));
54+
}
55+
56+
public void updateSingle(int index, int value) {
57+
array[index] = value;
58+
barColours[index] = 100;
59+
repaint();
60+
sleepFor(millisecondsToNano(15));
5361
}
5462

5563
/**

src/main/java/sortVisualiser/SortVisualiser.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import javax.swing.JFrame;
55
import sortVisualiser.algorithms.BubbleSort;
66
import sortVisualiser.algorithms.ISortAlgorithm;
7+
import sortVisualiser.algorithms.InsertionSort;
8+
import sortVisualiser.algorithms.SelectionSort;
79
import sortVisualiser.algorithms.Shuffle;
810
import static util.Sleep.secondsToNano;
911
import static util.Sleep.sleepFor;
@@ -32,6 +34,8 @@ public SortVisualiser() {
3234
window.setVisible(true);
3335

3436
sortQueue = new ArrayList<>();
37+
sortQueue.add(new SelectionSort());
38+
sortQueue.add(new InsertionSort());
3539
sortQueue.add(new BubbleSort());
3640
}
3741

@@ -49,7 +53,7 @@ private void shuffleAndWait() {
4953

5054
public void run() {
5155
for (ISortAlgorithm algorithm : sortQueue) {
52-
sleepFor(secondsToNano(2));
56+
sleepFor(secondsToNano(1));
5357
shuffleAndWait();
5458
algorithm.runSort(sortArray);
5559
sortArray.resetColours();
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package sortVisualiser.algorithms;
2+
3+
import sortVisualiser.SortArray;
4+
5+
/**
6+
* Insertion sort implementation
7+
* @author Matthew Hopson
8+
*/
9+
public class InsertionSort implements ISortAlgorithm
10+
{
11+
12+
@Override
13+
public void runSort(SortArray array) {
14+
for (int i = 0; i < array.arraySize(); i++) {
15+
int key = array.getValue(i);
16+
int j = i - 1;
17+
while (j >= 0 && array.getValue(j) > key) {
18+
array.updateSingle(j + 1, array.getValue(j));
19+
j--;
20+
}
21+
array.updateSingle(j + 1, key);
22+
}
23+
}
24+
25+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package sortVisualiser.algorithms;
2+
3+
import sortVisualiser.SortArray;
4+
5+
/**
6+
* Selection sort implementation
7+
* @author Matt Hopson
8+
*/
9+
public class SelectionSort implements ISortAlgorithm
10+
{
11+
@Override
12+
public void runSort(SortArray array) {
13+
for (int i = 0; i < array.arraySize() - 1; i++) {
14+
int minIndex = i;
15+
for (int j = i + 1; j < array.arraySize(); j++) {
16+
if (array.getValue(j) < array.getValue(minIndex)) {
17+
minIndex = j;
18+
}
19+
}
20+
array.swapUpdate(i, minIndex);
21+
}
22+
}
23+
}

0 commit comments

Comments
 (0)