Skip to content

Commit

Permalink
Insertion sort implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
hugh5 committed Nov 1, 2022
1 parent 26a243c commit 77b0f01
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class MainWindow implements ActionListener, ChangeListener {
private final JFrame window;
private MenuPanel menuPanel;
private SortingPanel sortingPanel;
public static final int MAX = 48;
public static final int MAX = 64;
public static final int MIN = 2;


Expand Down
54 changes: 26 additions & 28 deletions src/MenuPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ public class MenuPanel {
private JButton generate;
private JSlider length;
private JLabel lengthLabel;
private JComboBox<String> sortingMethod;
private JComboBox<SortingMethod> sortingMethod;
private JButton start;
private JButton forward;
private JButton backward;
private JSlider speed;
private JLabel speedLabel;

private static final String[] sorts = new String[]{"Selection Sort"};

public MenuPanel() {
createNorthPanel();
createSouthPanel();
Expand Down Expand Up @@ -46,23 +44,23 @@ private void createNorthPanel() {
northPanel.add(lengthLabel);
lengthLabel.setText("Length:" + length.getValue());

sortingMethod = new JComboBox<>(sorts);
sortingMethod = new JComboBox<>(SortingMethod.values());
northPanel.add(sortingMethod);
}

private void createSouthPanel() {
southPanel = new JPanel();
southPanel.setLayout(new FlowLayout(FlowLayout.CENTER));

URL backURL = getClass().getResource("assets/backward.png");
if (backURL != null) {
ImageIcon backIcon = new ImageIcon(backURL);
backIcon = new ImageIcon(backIcon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
backward = new JButton(backIcon);
} else {
backward = new JButton("Step Backward");
}
southPanel.add(backward);
// URL backURL = getClass().getResource("assets/backward.png");
// if (backURL != null) {
// ImageIcon backIcon = new ImageIcon(backURL);
// backIcon = new ImageIcon(backIcon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
// backward = new JButton(backIcon);
// } else {
// backward = new JButton("Step Backward");
// }
// southPanel.add(backward);

URL startURL = getClass().getResource("assets/start.png");
if (startURL != null) {
Expand All @@ -74,15 +72,15 @@ private void createSouthPanel() {
}
southPanel.add(start);

URL forwardURL = getClass().getResource("assets/forward.png");
if (forwardURL != null) {
ImageIcon forwardIcon = new ImageIcon(forwardURL);
forwardIcon = new ImageIcon(forwardIcon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
forward = new JButton(forwardIcon);
} else {
forward = new JButton("Step Forward");
}
southPanel.add(forward);
// URL forwardURL = getClass().getResource("assets/forward.png");
// if (forwardURL != null) {
// ImageIcon forwardIcon = new ImageIcon(forwardURL);
// forwardIcon = new ImageIcon(forwardIcon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
// forward = new JButton(forwardIcon);
// } else {
// forward = new JButton("Step Forward");
// }
// southPanel.add(forward);

speed = new JSlider(1, 10);
southPanel.add(speed);
Expand All @@ -100,8 +98,8 @@ public JPanel getSouthPanel() {
return southPanel;
}

public String getSortingMethod() {
return (String) sortingMethod.getSelectedItem();
public SortingMethod getSortingMethod() {
return (SortingMethod) sortingMethod.getSelectedItem();
}

public void addChangeListeners(ChangeListener changeListener) {
Expand All @@ -120,9 +118,9 @@ public void addActionListeners(ActionListener actionListener) {

start.setActionCommand("start");
start.addActionListener(actionListener);
backward.setActionCommand("backward");
backward.addActionListener(actionListener);
forward.setActionCommand("forward");
forward.addActionListener(actionListener);
// backward.setActionCommand("backward");
// backward.addActionListener(actionListener);
// forward.setActionCommand("forward");
// forward.addActionListener(actionListener);
}
}
4 changes: 4 additions & 0 deletions src/SortingMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
public enum SortingMethod {
SELECTION,
INSERTION
}
44 changes: 38 additions & 6 deletions src/SortingPanel.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import javax.swing.*;
import java.awt.*;
import java.lang.reflect.InvocationTargetException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -53,9 +52,13 @@ public void generateArray() {
generateArray(this.size);
}

public void start(String sortingMethod) {
System.out.println(this.getClass().getSimpleName() + "." + sortingMethod.replace(" ", "") + "()");
selectionSort();
public void start(SortingMethod sortingMethod) {
System.out.println("Starting: " + sortingMethod);
if (sortingMethod == SortingMethod.SELECTION) {
selectionSort();
} else if (sortingMethod == SortingMethod.INSERTION) {
insertionSort();
}
}

public void setSpeed(int speed) {
Expand Down Expand Up @@ -83,7 +86,11 @@ private void selectionSort() {
map.put(i, MIN);
minIndex = i;
} else {
map.put(i, CURRENT);
if (i == numSorted) {
map.put(i, MIN);
} else {
map.put(i, CURRENT);
}
}
updateScreen();
if (map.get(i) == CURRENT) map.remove(i);
Expand All @@ -94,7 +101,32 @@ private void selectionSort() {
map.remove(minIndex);
map.put(numSorted, SORTED);
numSorted++;
repaint();
updateScreen();
}
}

private void insertionSort() {
map.clear();
map.put(0, SORTED);
for (int n = 1; n < size; n++) {
for (int i = n; i > 0; i--) {
if (array[i] < array[i - 1]) {
map.put(i - 1, MIN);
map.put(i, CURRENT);
updateScreen();
map.put(i-1, SORTED);
map.put(i, SORTED);
int temp = array[i - 1];
array[i - 1] = array[i];
array[i] = temp;
} else {
map.put(i, SORTED);
updateScreen();
break;
}
}
updateScreen();
}
repaint();
}
}

0 comments on commit 77b0f01

Please sign in to comment.