summaryrefslogtreecommitdiff
path: root/labs/lab08_gnome_sort
diff options
context:
space:
mode:
authorAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
committerAlexander Pickering <alexandermpickering@gmail.com>2017-02-06 11:41:36 -0500
commit89cdf3efb49335e7c07a68a5a64657eeec2288a6 (patch)
treecdc0fd8165e65b1637fa54cac11c932acefc8a89 /labs/lab08_gnome_sort
downloadcoe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.tar.gz
coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.tar.bz2
coe0445-89cdf3efb49335e7c07a68a5a64657eeec2288a6.zip
Inital commitHEADmaster
Diffstat (limited to 'labs/lab08_gnome_sort')
-rw-r--r--labs/lab08_gnome_sort/SortingFrame.java106
-rw-r--r--labs/lab08_gnome_sort/VisualSortingComponent.java78
-rw-r--r--labs/lab08_gnome_sort/lab08_gnome_sort.pdfbin0 -> 101250 bytes
3 files changed, 184 insertions, 0 deletions
diff --git a/labs/lab08_gnome_sort/SortingFrame.java b/labs/lab08_gnome_sort/SortingFrame.java
new file mode 100644
index 0000000..6ceb49e
--- /dev/null
+++ b/labs/lab08_gnome_sort/SortingFrame.java
@@ -0,0 +1,106 @@
+import java.util.Random;
+
+import javax.swing.JFrame;
+
+public class SortingFrame
+{
+ public static void main(String[] args) throws InterruptedException
+ {
+ JFrame frame = new JFrame();
+
+ int[] data = randomIntArray(40);
+ VisualSortingComponent vsc = new VisualSortingComponent(data);
+ frame.setTitle("Sorting Visualization");
+ frame.setSize(500,500);
+ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
+ frame.add(vsc);
+ frame.setVisible(true);
+
+ Thread.sleep(50);
+
+ //bubbleSort(data,vsc);
+ gnomeSort(data,vsc);
+
+ }
+
+ public static void bubbleSort(int[] data, VisualSortingComponent vsc) throws InterruptedException
+ {
+ int size = data.length;
+
+ for(int i = 0; i < size; i++)
+ {
+ for(int j = 0; j < size - 1; j++)
+ {
+ if(data[j] > data[j + 1])
+ {
+ int temp = data[j];
+ data[j] = data[j + 1];
+ data[j + 1] = temp;
+ vsc.repaint();
+ Thread.sleep(10);
+ }
+ }
+ }
+ }
+
+ public static void gnomeSort(int[] data, VisualSortingComponent vsc) throws InterruptedException
+ {
+ int last = data[0];
+ for(int i = 1; i < data.length; i++)
+ {
+ //System.out.println("Sorting " + i);
+ if(data[i] < last)
+ {
+ //System.out.println("\tDoing work");
+ //The data is out of order, do some work
+ int j = i;
+ while(j>0)
+ {
+ if(data[j-1]>data[j])
+ {
+ //System.out.println("\tSwaping " + (j-1) + " and " + j);
+ data = swap(data,j-1,j);
+ j--;
+ vsc.repaint();
+ Thread.sleep(10);
+ }
+ else
+ {
+ break;
+ }
+ }
+ }
+ last = data[i];
+ }
+ }
+ private static int[] swap(int[] data, int ind1, int ind2)
+ {
+ int tmp = data[ind1];
+ data[ind1] = data[ind2];
+ data[ind2] = tmp;
+ return data;
+ }
+
+ public static int[] randomIntArray(int size)
+ {
+ int[] result = new int[size];
+
+ for(int i = 1; i <= size; i++)
+ {
+ result[i - 1] = i;
+ }
+
+ Random rand = new Random();
+
+ for(int i = 0; i < size * 100; i++)
+ {
+ int first = rand.nextInt(size);
+ int second = rand.nextInt(size);
+ int temp = result[first];
+ result[first] = result[second];
+ result[second] = temp;
+ }
+
+ return result;
+ }
+}
diff --git a/labs/lab08_gnome_sort/VisualSortingComponent.java b/labs/lab08_gnome_sort/VisualSortingComponent.java
new file mode 100644
index 0000000..92152b8
--- /dev/null
+++ b/labs/lab08_gnome_sort/VisualSortingComponent.java
@@ -0,0 +1,78 @@
+import java.awt.Color;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.geom.Line2D;
+import java.awt.geom.Rectangle2D;
+import javax.swing.JComponent;
+
+@SuppressWarnings("serial")
+public class VisualSortingComponent extends JComponent
+{
+ private int leftMargin = 10;
+ private int rightMargin = 10;
+ private int topMargin = 10;
+ private int bottomMargin = 10;
+ private int max;
+ private int min;
+ private int width;
+ private int height;
+ private int[] data;
+ private int numData;
+
+ public VisualSortingComponent(int[] aData)
+ {
+ data = aData;
+ numData = data.length;
+
+ if(numData > 0)
+ {
+ max = data[0];
+ min = data[0];
+
+ for(int i = 1; i < numData; i++)
+ {
+ if(data[i] > max)
+ {
+ max = data[i];
+ }
+
+ if(min > data[i])
+ {
+ min = data[i];
+ }
+ }
+ }
+ }
+
+ public void paintComponent(Graphics g)
+ {
+ Graphics2D g2 = (Graphics2D) g;
+
+ width = this.getWidth();
+ height = this.getHeight();
+
+ double dataWidth = (width - (leftMargin + rightMargin)) / numData;
+ double heightFactor = (height - (topMargin + bottomMargin)) / numData;
+
+ // draw the base;
+
+ Line2D.Double line = new Line2D.Double(leftMargin, height - bottomMargin, width - leftMargin, height - bottomMargin);
+ g2.draw(line);
+
+ // draw aata
+
+ Rectangle2D.Double rect = new Rectangle2D.Double(0,0,0,0);
+
+ for(int i = 0; i < numData; i++)
+ {
+ double x = leftMargin + (dataWidth * i);
+ double y = (height - bottomMargin) - (heightFactor * data[i]);
+ rect.setRect(x,y,dataWidth,data[i] * heightFactor);
+
+ g2.setColor(Color.GREEN);
+ g2.fill(rect);
+ g2.setColor(Color.BLACK);
+ g2.draw(rect);
+ }
+ }
+}
diff --git a/labs/lab08_gnome_sort/lab08_gnome_sort.pdf b/labs/lab08_gnome_sort/lab08_gnome_sort.pdf
new file mode 100644
index 0000000..24d7d01
--- /dev/null
+++ b/labs/lab08_gnome_sort/lab08_gnome_sort.pdf
Binary files differ