Skip to content

Commit 3b425d8

Browse files
author
bjrara
committed
init commit hw7
1 parent c1febd5 commit 3b425d8

File tree

6 files changed

+427
-0
lines changed

6 files changed

+427
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
package com.offline.training.homework.hw7;
2+
3+
/* ListSorts.java */
4+
5+
import com.offline.training.homework.hw7.list.*;
6+
7+
public class ListSorts {
8+
9+
private final static int SORTSIZE = 1000;
10+
11+
/**
12+
* makeQueueOfQueues() makes a queue of queues, each containing one item
13+
* of q. Upon completion of this method, q is empty.
14+
* @param q is a LinkedQueue of objects.
15+
* @return a LinkedQueue containing LinkedQueue objects, each of which
16+
* contains one object from q.
17+
**/
18+
public static LinkedQueue makeQueueOfQueues(LinkedQueue q) {
19+
// Replace the following line with your solution.
20+
return null;
21+
}
22+
23+
/**
24+
* mergeSortedQueues() merges two sorted queues into a third. On completion
25+
* of this method, q1 and q2 are empty, and their items have been merged
26+
* into the returned queue.
27+
* @param q1 is LinkedQueue of Comparable objects, sorted from smallest
28+
* to largest.
29+
* @param q2 is LinkedQueue of Comparable objects, sorted from smallest
30+
* to largest.
31+
* @return a LinkedQueue containing all the Comparable objects from q1
32+
* and q2 (and nothing else), sorted from smallest to largest.
33+
**/
34+
public static LinkedQueue mergeSortedQueues(LinkedQueue q1, LinkedQueue q2) {
35+
// Replace the following line with your solution.
36+
return null;
37+
}
38+
39+
/**
40+
* partition() partitions qIn using the pivot item. On completion of
41+
* this method, qIn is empty, and its items have been moved to qSmall,
42+
* qEquals, and qLarge, according to their relationship to the pivot.
43+
* @param qIn is a LinkedQueue of Comparable objects.
44+
* @param pivot is a Comparable item used for partitioning.
45+
* @param qSmall is a LinkedQueue, in which all items less than pivot
46+
* will be enqueued.
47+
* @param qEquals is a LinkedQueue, in which all items equal to the pivot
48+
* will be enqueued.
49+
* @param qLarge is a LinkedQueue, in which all items greater than pivot
50+
* will be enqueued.
51+
**/
52+
public static void partition(LinkedQueue qIn, Comparable pivot,
53+
LinkedQueue qSmall, LinkedQueue qEquals,
54+
LinkedQueue qLarge) {
55+
// Your solution here.
56+
}
57+
58+
/**
59+
* mergeSort() sorts q from smallest to largest using mergesort.
60+
* @param q is a LinkedQueue of Comparable objects.
61+
**/
62+
public static void mergeSort(LinkedQueue q) {
63+
// Your solution here.
64+
}
65+
66+
/**
67+
* quickSort() sorts q from smallest to largest using quicksort.
68+
* @param q is a LinkedQueue of Comparable objects.
69+
**/
70+
public static void quickSort(LinkedQueue q) {
71+
// Your solution here.
72+
}
73+
74+
/**
75+
* makeRandom() builds a LinkedQueue of the indicated size containing
76+
* Integer items. The items are randomly chosen between 0 and size - 1.
77+
* @param size is the size of the resulting LinkedQueue.
78+
**/
79+
public static LinkedQueue makeRandom(int size) {
80+
LinkedQueue q = new LinkedQueue();
81+
for (int i = 0; i < size; i++) {
82+
q.enqueue(new Integer((int) (size * Math.random())));
83+
}
84+
return q;
85+
}
86+
87+
/**
88+
* main() performs some tests on mergesort and quicksort. Feel free to add
89+
* more tests of your own to make sure your algorithms works on boundary
90+
* cases. Your test code will not be graded.
91+
**/
92+
public static void main(String [] args) {
93+
94+
LinkedQueue q = makeRandom(10);
95+
System.out.println(q.toString());
96+
mergeSort(q);
97+
System.out.println(q.toString());
98+
99+
q = makeRandom(10);
100+
System.out.println(q.toString());
101+
quickSort(q);
102+
System.out.println(q.toString());
103+
104+
/* Remove these comments for Part III.
105+
Timer stopWatch = new Timer();
106+
q = makeRandom(SORTSIZE);
107+
stopWatch.start();
108+
mergeSort(q);
109+
stopWatch.stop();
110+
System.out.println("Mergesort time, " + SORTSIZE + " Integers: " +
111+
stopWatch.elapsed() + " msec.");
112+
113+
stopWatch.reset();
114+
q = makeRandom(SORTSIZE);
115+
stopWatch.start();
116+
quickSort(q);
117+
stopWatch.stop();
118+
System.out.println("Quicksort time, " + SORTSIZE + " Integers: " +
119+
stopWatch.elapsed() + " msec.");
120+
*/
121+
}
122+
123+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.offline.training.homework.hw7;
2+
3+
/* Timer.java */
4+
5+
/**
6+
* Implements a simple stopwatch/timer class based on wall-clock time.
7+
**/
8+
9+
/**
10+
* RUNNING() == true <==> start() called with no corresponding
11+
* call to stop()
12+
*
13+
* All times are given in units of msec.
14+
**/
15+
public class Timer {
16+
17+
private boolean running;
18+
private long tStart;
19+
private long tFinish;
20+
private long tAccum;
21+
22+
/**
23+
* Initializes Timer to 0 msec
24+
**/
25+
public Timer() {
26+
reset();
27+
}
28+
29+
/**
30+
* Starts the timer. Accumulates time across multiple calls to start.
31+
**/
32+
public void start() {
33+
running = true;
34+
tStart = System.currentTimeMillis();
35+
tFinish = tStart;
36+
}
37+
38+
/**
39+
* Stops the timer. returns the time elapsed since the last matching call
40+
* to start(), or zero if no such matching call was made.
41+
**/
42+
public long stop() {
43+
tFinish = System.currentTimeMillis();
44+
if (running) {
45+
running = false;
46+
47+
long diff = tFinish - tStart;
48+
tAccum += diff;
49+
return diff;
50+
}
51+
return 0;
52+
}
53+
54+
/**
55+
* if RUNNING() ==> returns the time since last call to start()
56+
* if !RUNNING() ==> returns total elapsed time
57+
**/
58+
public long elapsed() {
59+
if (running) {
60+
return System.currentTimeMillis() - tStart;
61+
}
62+
63+
return tAccum;
64+
}
65+
66+
/**
67+
* Stops timing, if currently RUNNING(); resets
68+
* accumulated time to 0.
69+
*/
70+
public void reset() {
71+
running = false;
72+
tStart = 0;
73+
tFinish = 0;
74+
tAccum = 0;
75+
}
76+
77+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
/* LinkedQueue.java */
2+
3+
package com.offline.training.homework.hw7.list;
4+
5+
public class LinkedQueue implements Queue {
6+
7+
private SListNode head;
8+
private SListNode tail;
9+
private int size;
10+
11+
/**
12+
* LinkedQueue() constructs an empty queue.
13+
**/
14+
public LinkedQueue() {
15+
size = 0;
16+
head = null;
17+
tail = null;
18+
}
19+
20+
/**
21+
* size() returns the size of this Queue.
22+
* @return the size of this Queue.
23+
* Performance: runs in O(1) time.
24+
**/
25+
public int size() {
26+
return size;
27+
}
28+
29+
/**
30+
* isEmpty() returns true if this Queue is empty, false otherwise.
31+
* @return true if this Queue is empty, false otherwise.
32+
* Performance: runs in O(1) time.
33+
**/
34+
public boolean isEmpty() {
35+
return size == 0;
36+
}
37+
38+
/**
39+
* enqueue() inserts an object at the end of the Queue.
40+
* @param item the item to be enqueued.
41+
**/
42+
public void enqueue(Object item) {
43+
if (head == null) {
44+
head = new SListNode(item);
45+
tail = head;
46+
} else {
47+
tail.next = new SListNode(item);
48+
tail = tail.next;
49+
}
50+
size++;
51+
}
52+
53+
/**
54+
* dequeue() removes and returns the object at the front of the Queue.
55+
* @return the item dequeued.
56+
* @throws a QueueEmptyException if the Queue is empty.
57+
**/
58+
public Object dequeue() throws QueueEmptyException {
59+
if (head == null) {
60+
throw new QueueEmptyException();
61+
} else {
62+
Object o = head.item;
63+
head = head.next;
64+
size--;
65+
if (size == 0) {
66+
tail = null;
67+
}
68+
return o;
69+
}
70+
}
71+
72+
/**
73+
* front() returns the object at the front of the Queue.
74+
* @return the item at the front of the Queue.
75+
* @throws a QueueEmptyException if the Queue is empty.
76+
**/
77+
public Object front() throws QueueEmptyException {
78+
if (head == null) {
79+
throw new QueueEmptyException();
80+
} else {
81+
return head.item;
82+
}
83+
}
84+
85+
/**
86+
*
87+
* nth() returns the nth item in this LinkedQueue.
88+
* Items in the queue are numbered from 1.
89+
* @param n the number of the item to return.
90+
*/
91+
public Object nth(int n) {
92+
SListNode node = head;
93+
for (; n > 1; n--) {
94+
node = node.next;
95+
}
96+
return node.item;
97+
}
98+
99+
/**
100+
* append() appends the contents of q onto the end of this LinkedQueue.
101+
* On completion, q is empty.
102+
* @param q the LinkedQueue whose contents should be appended onto this
103+
* LinkedQueue.
104+
**/
105+
public void append(LinkedQueue q) {
106+
if (head == null) {
107+
head = q.head;
108+
} else {
109+
tail.next = q.head;
110+
}
111+
if (q.head != null) {
112+
tail = q.tail;
113+
}
114+
size = size + q.size;
115+
q.head = null;
116+
q.tail = null;
117+
q.size = 0;
118+
}
119+
120+
/**
121+
* toString() converts this queue to a String.
122+
**/
123+
public String toString() {
124+
String out = "[ ";
125+
try {
126+
for (int i = 0; i < size(); i++) {
127+
out = out + front() + " ";
128+
enqueue(dequeue());
129+
}
130+
} catch (QueueEmptyException uf) {
131+
System.err.println("Error: attempt to dequeue from empty queue.");
132+
}
133+
return out + "]";
134+
}
135+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* Queue.java */
2+
3+
package com.offline.training.homework.hw7.list;
4+
5+
public interface Queue {
6+
7+
/**
8+
* size() returns the size of this Queue.
9+
* @return the size of this Queue.
10+
* Performance: runs in O(1) time.
11+
**/
12+
public int size();
13+
14+
/**
15+
* isEmpty() returns true if this Queue is empty, false otherwise.
16+
* @return true if this Queue is empty, false otherwise.
17+
* Performance: runs in O(1) time.
18+
**/
19+
public boolean isEmpty();
20+
21+
/**
22+
* enqueue() inserts an object at the end of the Queue.
23+
* @param item the item to be enqueued.
24+
**/
25+
public void enqueue(Object item);
26+
27+
/**
28+
* dequeue() removes and returns the object at the front of the Queue.
29+
* @return the item dequeued.
30+
* @throws a QueueEmptyException if the Queue is empty.
31+
**/
32+
public Object dequeue() throws QueueEmptyException;
33+
34+
/**
35+
* front() returns the object at the front of the Queue.
36+
* @return the item at the front of the Queue.
37+
* @throws a QueueEmptyException if the Queue is empty.
38+
**/
39+
public Object front() throws QueueEmptyException;
40+
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/* QueueEmptyException.java */
2+
3+
package com.offline.training.homework.hw7.list;
4+
5+
public class QueueEmptyException extends Exception {
6+
7+
public QueueEmptyException() {
8+
super();
9+
}
10+
11+
public QueueEmptyException(String s) {
12+
super(s);
13+
}
14+
15+
}

0 commit comments

Comments
 (0)