Patel
Patel
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
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<Integer> 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 <
if (!d1.isEmpty
for (int i = 0;
for (int i = 0;
for (int i = 0;
())
i <
i <
i <
())
i <
i <
i <
if (!d1.isEmpty
for (int i = 0;
for (int i = 0;
for (int i = 0;
())
i <
i <
i <
try {
d1.popRight ();
throw new Error ("Expected exception");
} catch (NoSuchElementException e) {}
}
private static double timeTrial(int N) {
int NUM_TRIALS = 10;
MyDequeUsingStacks<Integer> 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<N; i++) {
d1.popLeft ();
d1.popRight ();
}
}
return sw.elapsedTime ();
}
// Once you have something that works faster, you can use a higher value for MIN.
// You will get better experimental numbers, since the garbage collector is less
// likely to run.
//
// private static final int MIN = 512000;
//
private static final int MIN = 2000;
private static final int MAX = 4096000;
public static void main (String args[]) {
-4-
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-
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-
-3-
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-
-2-