Skip to content

Commit

Permalink
add gnome sort
Browse files Browse the repository at this point in the history
  • Loading branch information
Hopson97 committed Sep 4, 2018
1 parent 119d499 commit f4cef97
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/main/java/sortVisualiser/SortVisualiser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.ArrayList;
import javax.swing.JFrame;
import sortVisualiser.algorithms.BubbleSort;
import sortVisualiser.algorithms.GnomeSort;
import sortVisualiser.algorithms.ISortAlgorithm;
import sortVisualiser.algorithms.InsertionSort;
import sortVisualiser.algorithms.MergeSort;
Expand Down Expand Up @@ -33,7 +34,8 @@ public SortVisualiser() {
window.setVisible(true);

sortQueue = new ArrayList<>();
sortQueue.add(new MergeSort());
sortQueue.add(new GnomeSort());
//sortQueue.add(new MergeSort());
//sortQueue.add(new QuickSort());
//sortQueue.add(new SelectionSort());
//sortQueue.add(new InsertionSort());
Expand Down
36 changes: 36 additions & 0 deletions src/main/java/sortVisualiser/algorithms/GnomeSort.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package sortVisualiser.algorithms;

import sortVisualiser.SortArray;

/**
*
* @author Matthew Hopson
*/
public class GnomeSort implements ISortAlgorithm {
@Override
public void runSort(SortArray array) {
int index = 0;
while (index < array.arraySize()) {
if (index == 0) {
index++;
}
if (array.getValue(index) >= array.getValue(index - 1)) {
index++;
}
else {
array.swap(index, index - 1, getDelay());
index--;
}
}
}

@Override
public String getName() {
return "Shell sort";
}

@Override
public long getDelay() {
return 5;
}
}

0 comments on commit f4cef97

Please sign in to comment.