Skip to content

Commit f4cef97

Browse files
committed
add gnome sort
1 parent 119d499 commit f4cef97

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed

src/main/java/sortVisualiser/SortVisualiser.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.ArrayList;
44
import javax.swing.JFrame;
55
import sortVisualiser.algorithms.BubbleSort;
6+
import sortVisualiser.algorithms.GnomeSort;
67
import sortVisualiser.algorithms.ISortAlgorithm;
78
import sortVisualiser.algorithms.InsertionSort;
89
import sortVisualiser.algorithms.MergeSort;
@@ -33,7 +34,8 @@ public SortVisualiser() {
3334
window.setVisible(true);
3435

3536
sortQueue = new ArrayList<>();
36-
sortQueue.add(new MergeSort());
37+
sortQueue.add(new GnomeSort());
38+
//sortQueue.add(new MergeSort());
3739
//sortQueue.add(new QuickSort());
3840
//sortQueue.add(new SelectionSort());
3941
//sortQueue.add(new InsertionSort());
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package sortVisualiser.algorithms;
2+
3+
import sortVisualiser.SortArray;
4+
5+
/**
6+
*
7+
* @author Matthew Hopson
8+
*/
9+
public class GnomeSort implements ISortAlgorithm {
10+
@Override
11+
public void runSort(SortArray array) {
12+
int index = 0;
13+
while (index < array.arraySize()) {
14+
if (index == 0) {
15+
index++;
16+
}
17+
if (array.getValue(index) >= array.getValue(index - 1)) {
18+
index++;
19+
}
20+
else {
21+
array.swap(index, index - 1, getDelay());
22+
index--;
23+
}
24+
}
25+
}
26+
27+
@Override
28+
public String getName() {
29+
return "Shell sort";
30+
}
31+
32+
@Override
33+
public long getDelay() {
34+
return 5;
35+
}
36+
}

0 commit comments

Comments
 (0)