C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 6\MyDequeUsingStacks 8, 2016 8:54 PM (
// Exercise 1.4.31 package algs14; import stdlib.*; import java.util.NoSuchElementException; import algs13.ResizingArrayStack; /* Here is the kind of output I get before fixing the move method. 4000 8000 16000 32000
0.4 1.5 4.9 21.6
3.1 4.1 3.2 4.4
Here is the kind of output I get after fixing the move method. You can see that it is much faster, but not very consistent. This is due to garbage collection and other system effects. 4000 8000 16000 32000 64000 128000 256000 512000 1024000 2048000 4096000
0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.2 0.4 0.7 2.5
0.7 1.2 2.2 0.2 1.5 5.7 5.3 2.1 1.9 2.0 3.6
4000 8000 16000 32000 64000 128000 256000 512000 1024000 2048000 4096000
0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.2 0.3 0.7 3.7
0.8 1.6 1.8 0.1 2.0 4.5 5.7 1.8 1.8 2.1 5.1
4000 8000 16000 32000 64000 128000 256000 512000 1024000 2048000 4096000 */
0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.1 0.1 0.1 0.2
0.7 2.0 1.9 0.2 1.7 4.4 1.6 2.3 0.6 2.0 2.4
public class MyDequeUsingStacks
{ ResizingArrayStack sl = new ResizingArrayStack<>(); ResizingArrayStack sr = new ResizingArrayStack<>(); public boolean isEmpty () { return sl.isEmpty() && sr.isEmpty(); } public int size () { return sl.size() + sr.size(); } public void pushLeft (T item) { sl.push (item); } public void pushRight (T item) { sr.push (item); } -1-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 6\MyDequeUsingStacks 8, 2016 8:54 PM (
private void move (ResizingArrayStack from, ResizingArrayStack to) { if (!to.isEmpty ()) throw new IllegalArgumentException(); if (from.isEmpty ()) throw new IllegalArgumentException(); // while (!from.isEmpty ()) // to.push (from.pop ()); // TODO: Fix this method so that the overall performance is not so bad! // You can use one temporary stack to help you: ResizingArrayStack tmp = new ResizingArrayStack<>(); // declear the int number here. int numbers = from.size(); // size // here set up for loop for array stack to go in. for (int numbersinlist = 0; numbersinlist < numbers/2; numbersinlist++) { tmp.push(from.pop()); } // here set up while !from and !temp. while(!from.isEmpty()) { to.push(from.pop()); } while (!tmp.isEmpty()) { from.push(tmp.pop()); } // You may find it helpful to print the sizes while debugging: // StdOut.println ("sizes: " + from.size () + " " + to.size ()); } public T popLeft () { if (isEmpty()) { throw new NoSuchElementException(); } if (sl.isEmpty ()) { move (sr, sl); } return sl.pop (); } public T popRight () { if (isEmpty()) { throw new NoSuchElementException(); } if (sr.isEmpty ()) { move (sl, sr); } return sr.pop (); } public String toString () { if (isEmpty ()) return "[ ]"; ResizingArrayStack srBackwards = new ResizingArrayStack<>(); for (T item : sr) srBackwards.push (item); final StringBuilder sb = new StringBuilder ("[ "); for (T item : sl) { sb.append (item); sb.append (" "); } for (T item : srBackwards) { sb.append (item); sb.append (" "); } sb.append ("]"); return sb.toString (); } private void check (String expected) { -2-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 6\MyDequeUsingStacks 8, 2016 8:54 PM (
if (expected != null) { if (!expected.equals (this.toString ())) throw new Error ("Expected \"" + expected + "\", got \"" + this + "\""); } StdOut.println (this); } private void check (T iExpected, T iActual, String expected) { if (!iExpected.equals (iActual)) throw new Error ("Expected \"" + iExpected + "\", got \"" + iActual + "\""); check (expected); } private void check (T iExpected, T iActual) { if (!iExpected.equals (iActual)) throw new Error ("Expected \"" + iExpected + "\", got \"" + iActual + "\""); } private static void correctnessTest () { MyDequeUsingStacks d1 = new MyDequeUsingStacks<> (); d1.pushLeft(0); d1.pushLeft(1); d1.pushLeft(2); d1.pushLeft(3); d1.check(0,d1.popRight()); d1.check(3,d1.popLeft()); d1.check(1,d1.popRight()); d1.check(2,d1.popLeft()); d1.pushRight(0); d1.pushRight(1); d1.pushRight(2); d1.pushRight(3); d1.check(0,d1.popLeft()); d1.check(3,d1.popRight()); d1.check(1,d1.popLeft()); d1.check(2,d1.popRight()); Integer k; if (!d1.isEmpty ()) throw new Error(); d1.pushLeft (11); d1.check ("[ 11 ]"); d1.pushLeft (12); d1.check ("[ 12 11 ]"); k = d1.popLeft (); d1.check (12, k, "[ 11 ]"); k = d1.popLeft (); d1.check (11, k, "[ ]"); try { d1.popLeft (); throw new Error ("Expected exception"); } catch (NoSuchElementException e) {} if (!d1.isEmpty ()) throw new Error(); for (int i = 0; i < 20; i++) { d1.pushLeft (i); } for (int i = 0; i < 20; i++) { d1.check (19-i, d1.popLeft ()); } if (!d1.isEmpty ()) throw new Error(); for (int i = 0; i < 20; i++) { d1.pushLeft (i); } for (int i = 0; i < 20; i++) { d1.check (i, d1.popRight ()); } if (!d1.isEmpty for (int i = 0; for (int i = 0; for (int i = 0;
()) i < i < i <
throw new Error(); 20; i++) { d1.pushLeft (i); } 10; i++) { d1.check (i, d1.popRight ()); } 10; i++) { d1.check (19-i, d1.popLeft ()); } -3-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 6\MyDequeUsingStacks 8, 2016 8:54 PM (
if (!d1.isEmpty for (int i = 0; for (int i = 0; for (int i = 0;
()) i < i < i <
throw new Error(); 20; i++) { d1.pushLeft (i); } 10; i++) { d1.check (19-i, d1.popLeft ()); } 10; i++) { d1.check (i, d1.popRight ()); }
if (!d1.isEmpty ()) throw new Error(); d1.pushRight (11); d1.check ("[ 11 ]"); d1.pushRight (12); d1.check ("[ 11 12 ]"); k = d1.popRight (); d1.check (12, k, "[ 11 ]"); k = d1.popRight (); d1.check (11, k, "[ ]"); if (!d1.isEmpty ()) throw new Error(); for (int i = 0; i < 20; i++) { d1.pushRight (i); } for (int i = 0; i < 20; i++) { d1.check (19-i, d1.popRight ()); } if (!d1.isEmpty ()) throw new Error(); for (int i = 0; i < 20; i++) { d1.pushRight (i); } for (int i = 0; i < 20; i++) { d1.check (i, d1.popLeft ()); } if (!d1.isEmpty for (int i = 0; for (int i = 0; for (int i = 0;
()) i < i < i <
throw new Error(); 20; i++) { d1.pushRight (i); } 10; i++) { d1.check (i, d1.popLeft ()); } 10; i++) { d1.check (19-i, d1.popRight ()); }
if (!d1.isEmpty for (int i = 0; for (int i = 0; for (int i = 0;
()) i < i < i <
throw new Error(); 20; i++) { d1.pushRight (i); } 10; i++) { d1.check (19-i, d1.popRight ()); } 10; i++) { d1.check (i, d1.popLeft ()); }
try { d1.popRight (); throw new Error ("Expected exception"); } catch (NoSuchElementException e) {} } private static double timeTrial(int N) { int NUM_TRIALS = 10; MyDequeUsingStacks d1 = new MyDequeUsingStacks<> (); Stopwatch sw = new Stopwatch (); for (int trial=0; trial < NUM_TRIALS; trial++) { for (int i=0; i<2*N; i++) { d1.pushLeft (i); } for (int i=0; i
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 6\MyDequeUsingStacks 8, 2016 8:54 PM (
correctnessTest (); double prev = timeTrial(MIN); for (int N = MIN*2; N<=MAX; N += N) { double time = timeTrial(N); StdOut.printf("%8d %9.3f %5.1f\n", N, time, time/prev); prev = time; } } }
-5-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 7\MyDeckSort 8, 2016 8:48 (ans).java PM
package algs21; import stdlib.*; // Exercise 2.1.14 /** * Complete the following method to sort a deck of cards, * with the restriction that the only allowed operations are to look * at the values of the top two cards, to exchange the top two cards, * and to move the top card to the bottom of the deck. */ public class MyDeckSort { public static void sort (MyDeck d) { // TODO // You must sort the Deck using only the public methods of Deck: // d.size (); // d.isSorted (); // d.topGreaterThanNext (); // d.swapTopTwo (); // While debugging, you will want to print intermediate results. // You can use d.toString() for that: // StdOut.printf ("i=%-3d %s\n", i, d.toString ()) // declare size here. int size = d.size(); // size //start for loops here. for (int deckOfCardsi = 0; deckOfCardsi < size; deckOfCardsi++) //going deck of cards repate. { //we can do two things for size start the at 1 in nested loop or size-1. it will be same things. for (int deckOfCardsj = 1; deckOfCardsj < size; deckOfCardsj++) { if (d.topGreaterThanNext()) { // if the top greater than use the swap. d.swapTopTwo(); // swap top and second card. d.moveTopToBottom(); // this moves top card to bottom. //StdOut.printf ("i=%-3d %s\n", deckOfCards, d.toString ()); } else { d.moveTopToBottom(); //StdOut.printf ("i=%-3d %s\n", deckOfCards, d.toString ()); } } d.moveTopToBottom(); } } private static double time; private static void countops (MyDeck d) { boolean print = true; if (print) StdOut.println (d.toString ()); d.moveTopToBottom (); if (print) StdOut.println (d.toString ()); Stopwatch sw = new Stopwatch (); sort (d); time = sw.elapsedTime (); if (print) StdOut.println (d.toString ()); d.isSorted (); } public static void main (String[] args) { int N = 10; MyDeck d = new MyDeck (N); countops (d); //System.exit (0); // Comment this out to do a doubling test! double prevOps = d.ops (); -1-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 7\MyDeckSort 8, 2016 8:48 (ans).java PM
double prevTime = time; for (int i = 0; i < 10; i++) { N *= 2; d = new MyDeck (N); countops (d); StdOut.printf ("%8d %10d %5.1f [%5.3f %5.3f]\n", N, d.ops (), d.ops () / prevOps, time, time / prevTime); prevOps = d.ops (); prevTime = time; } } } /** * The Deck class has the following API: * * * MyDeck (int N) // create a randomized Deck of size N * int size () // return the size of N * int ops () // return the number of operations performed on this Deck * boolean topGreaterThanNext () // compare top two items * void swapTopTwo () // swap top two itens * void moveTopToBottom () // move top item to bottom * void isSorted () // check if isSorted (throws exception if not) *
*/ class MyDeck { private int N; private int top; private long ops; private int[] a; public long ops () { return ops; } public int size () { return N; } public MyDeck (int N) { this.N = N; this.top = 0; this.ops = 0; this.a = new int[N]; for (int i = 0; i < N; i++) a[i] = i; StdRandom.shuffle (a); } public boolean topGreaterThanNext () { int i = a[top]; int j = a[(top + 1) % N]; ops += 2; return i > j; } public void swapTopTwo () { int i = a[top]; int j = a[(top + 1) % N]; a[top] = j; a[(top + 1) % N] = i; ops += 4; } public void moveTopToBottom () { top = (top + 1) % N; ops += 1; } -2-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 7\MyDeckSort 8, 2016 8:48 (ans).java PM
public String toString () { StringBuilder b = new StringBuilder (); b.append ('['); for (int i = top;;) { b.append (a[i]); i = (i + 1) % N; if (i == top) return b.append (']').toString (); b.append (", "); } } public void isSorted () { boolean print = false; long theOps = ops; // don't count the operations require by isSorted for (int i = 1; i < N; i++) { if (print) StdOut.printf ("i=%-3d %s\n", i, toString ()); if (topGreaterThanNext ()) throw new Error (); moveTopToBottom (); } if (print) StdOut.printf ("i=%-3d %s\n", N, toString ()); moveTopToBottom (); if (print) StdOut.printf ("i=%-3d %s\n", N + 1, toString ()); ops = theOps; } }
-3-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 9\MyLinkedSort 8, 2016 8:49 (ans).jav PM
package algs13; import stdlib.*; // PROBLEM 2.2.17 public class MyLinkedSort { static class Node { public Node() { } public double item; public Node next; } int N; Node first; public MyLinkedSort () { first = null; N = 0; checkInvariants (); } private void myassert (String s, boolean b) { if (!b) throw new Error ("Assertion failed: " + s); } private void checkInvariants() { myassert("Empty <==> first==null", (N == 0) == (first == null)); Node x = first; for (int i = 0; i < N; i++) { if (x==null) { throw new Error ("List too short!"); } x = x.next; } myassert("EndOfList == null", x == null); } public boolean isEmpty () { return first == null; } public int size () { return N; } public void add (double item) { Node newfirst = new Node (); newfirst.item = item; newfirst.next = first; first = newfirst; N++; } private static void print (String s, MyLinkedSort b) { StdOut.print (s + ": "); for (Node x = b.first; x != null; x = x.next) StdOut.print (x.item + " "); StdOut.println (); } private static void print (String s, MyLinkedSort b, double i) { StdOut.print (s + ": "); for (Node x = b.first; x != null; x = x.next) StdOut.print (x.item + " "); StdOut.println (": " + i); } private void sort(){ mergeSort(this); } private static MyLinkedSort split(MyLinkedSort splitINtwo){ Node temp = splitINtwo.first; // front node very first part int size = splitINtwo.size()/2; for (int i = 0; i < size; i++) -1-
C:\Users\arpan\Google Drive\DePaul University\DePual Classes\Spring 2015_2016\CSC_300 DATA STRUCTURES IN JAVA I\Assignments\Assignments Monday, August 9\MyLinkedSort 8, 2016 8:49 (ans).jav PM
temp = temp.next; //move the pointer next //now list for second half MyLinkedSort secondhalf = new MyLinkedSort(); // create new list start at mid point secondhalf.first = temp.next;// new list we starting at second half of the orginal temp.next = null; // cutting in half splitINtwo.N = splitINtwo.size()/2; // update the size return secondhalf; // this function is split half. divide by 2 } private static Node merge(Node left, Node right){ if(left == null) return right; if (right == null) return left; Node temp = null; if (left.item <= right.item){ // left item is less than right item temp= left; // update the left temp.next=merge(left.next,right); // recur calling the function }else{ temp =right; temp.next = merge(left, right.next); } return temp; } private static void mergeSort(MyLinkedSort inputlist){ if (inputlist.isEmpty() || inputlist.first.next==null) return; MyLinkedSort list = split(inputlist); // split in half mergeSort(inputlist); mergeSort(list); inputlist.first = merge(inputlist.first, list.first); } public static void main (String args[]) { int[] a1 = new int[20]; for (int i = 0; i < a1.length; i++) a1[i] = i; StdRandom.shuffle (a1); MyLinkedSort b1 = new MyLinkedSort (); for (int i:a1) b1.add(i); MyLinkedSort.print("before sort",b1); b1.sort(); MyLinkedSort.print("after sort",b1); int[] a2 = new int[200]; for (int i = 0; i < a2.length; i++) a2[i] = i; StdRandom.shuffle (a2); MyLinkedSort b2 = new MyLinkedSort (); for (int i:a2) b2.add(i); MyLinkedSort.print("before sort",b2); b2.sort(); MyLinkedSort.print("after sort",b2); } }
-2-