|
| 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 | +} |
0 commit comments