<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://www.lensovet.net/~sysadmin/w/index.php?action=history&amp;feed=atom&amp;title=Computer_Science%2F61b%2FHomework%2Fhw8</id>
	<title>Computer Science/61b/Homework/hw8 - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://www.lensovet.net/~sysadmin/w/index.php?action=history&amp;feed=atom&amp;title=Computer_Science%2F61b%2FHomework%2Fhw8"/>
	<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;action=history"/>
	<updated>2026-05-03T01:45:42Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.31.16</generator>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=24363&amp;oldid=prev</id>
		<title>Lensovet: Lensovet moved page CS/61b/Homework/hw8 to Computer Science/61b/Homework/hw8</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=24363&amp;oldid=prev"/>
		<updated>2023-02-20T03:51:35Z</updated>

		<summary type="html">&lt;p&gt;Lensovet moved page &lt;a href=&quot;/~sysadmin/w/CS/61b/Homework/hw8&quot; class=&quot;mw-redirect&quot; title=&quot;CS/61b/Homework/hw8&quot;&gt;CS/61b/Homework/hw8&lt;/a&gt; to &lt;a href=&quot;/~sysadmin/w/Computer_Science/61b/Homework/hw8&quot; title=&quot;Computer Science/61b/Homework/hw8&quot;&gt;Computer Science/61b/Homework/hw8&lt;/a&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 03:51, 20 February 2023&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=4051&amp;oldid=prev</id>
		<title>Lensovet: moved CS 61b/Homework/hw8 to CS/61b/Homework/hw8:&amp;#32;fix cs 61b hierarchy</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=4051&amp;oldid=prev"/>
		<updated>2010-11-14T06:00:20Z</updated>

		<summary type="html">&lt;p&gt;moved &lt;a href=&quot;/~sysadmin/w/CS_61b/Homework/hw8&quot; class=&quot;mw-redirect&quot; title=&quot;CS 61b/Homework/hw8&quot;&gt;CS 61b/Homework/hw8&lt;/a&gt; to &lt;a href=&quot;/~sysadmin/w/CS/61b/Homework/hw8&quot; class=&quot;mw-redirect&quot; title=&quot;CS/61b/Homework/hw8&quot;&gt;CS/61b/Homework/hw8&lt;/a&gt;: fix cs 61b hierarchy&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 06:00, 14 November 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-notice&quot; lang=&quot;en&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(No difference)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=3251&amp;oldid=prev</id>
		<title>Lensovet: fix grader links and add to filelist</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=3251&amp;oldid=prev"/>
		<updated>2007-09-22T06:53:41Z</updated>

		<summary type="html">&lt;p&gt;fix grader links and add to filelist&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;col class=&quot;diff-marker&quot; /&gt;
				&lt;col class=&quot;diff-content&quot; /&gt;
				&lt;tr class=&quot;diff-title&quot; lang=&quot;en&quot;&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;← Older revision&lt;/td&gt;
				&lt;td colspan=&quot;2&quot; style=&quot;background-color: #fff; color: #222; text-align: center;&quot;&gt;Revision as of 06:53, 22 September 2007&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l3&quot; &gt;Line 3:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 3:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*[[/Timer.java]]&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*[[/Timer.java]]&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*[[/list]] package&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;*[[/list]] package&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot;&gt;&amp;#160;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;&lt;ins style=&quot;font-weight: bold; text-decoration: none;&quot;&gt;*[[/GRADER]]&lt;/ins&gt;&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Grade==&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;==Grade==&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot; id=&quot;mw-diff-left-l120&quot; &gt;Line 120:&lt;/td&gt;
&lt;td colspan=&quot;2&quot; class=&quot;diff-lineno&quot;&gt;Line 121:&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;in the data structures the stability of the sort is decided--that is, whether&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;in the data structures the stability of the sort is decided--that is, whether&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;or not equal items are kept in their original order at critical parts of the&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;&amp;#160;&lt;/td&gt;&lt;td style=&quot;background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;or not equal items are kept in their original order at critical parts of the&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class='diff-marker'&gt;−&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;sorting process.&amp;#160; Put your answers in the [[/GRADER&lt;del class=&quot;diffchange diffchange-inline&quot;&gt;|&lt;/del&gt;]] file.&lt;/div&gt;&lt;/td&gt;&lt;td class='diff-marker'&gt;+&lt;/td&gt;&lt;td style=&quot;color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;&quot;&gt;&lt;div&gt;sorting process.&amp;#160; Put your answers in the [[/GRADER]] file.&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
	<entry>
		<id>http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=3230&amp;oldid=prev</id>
		<title>Lensovet at 06:16, 22 September 2007</title>
		<link rel="alternate" type="text/html" href="http://www.lensovet.net/~sysadmin/w/index.php?title=Computer_Science/61b/Homework/hw8&amp;diff=3230&amp;oldid=prev"/>
		<updated>2007-09-22T06:16:06Z</updated>

		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==Files==&lt;br /&gt;
*[[/ListSorts.java]]&lt;br /&gt;
*[[/Timer.java]]&lt;br /&gt;
*[[/list]] package&lt;br /&gt;
&lt;br /&gt;
==Grade==&lt;br /&gt;
&lt;br /&gt;
==Description==&lt;br /&gt;
                               CS 61B Homework 8&lt;br /&gt;
                       Due 4pm Friday, November 17, 2006&lt;br /&gt;
&lt;br /&gt;
This homework will give you practice implementing sorting algorithms, and will&lt;br /&gt;
illustrate how sorting linked lists can be different from sorting arrays.&lt;br /&gt;
This is an individual assignment; you may not share code with other students.&lt;br /&gt;
&lt;br /&gt;
Copy the Homework 8 directory by doing the following, starting from your home&lt;br /&gt;
directory.  Don't forget the &amp;quot;-r&amp;quot; switch in the cp command.&lt;br /&gt;
&lt;br /&gt;
    cp -r $master/hw/hw8 .&lt;br /&gt;
&lt;br /&gt;
Your job is to implement two sorting algorithms for linked lists.  The data&lt;br /&gt;
structure you will use is a catenable queue.  &amp;quot;Catenable&amp;quot; means that two queues&lt;br /&gt;
can be concatenated into a single queue efficiently--in O(1) time.  The&lt;br /&gt;
LinkedQueue data structure is implemented as a singly-linked list with a tail&lt;br /&gt;
pointer, much like the one you worked with in Lab 3.&lt;br /&gt;
&lt;br /&gt;
The LinkedQueue class (in the list package) has the following methods.&lt;br /&gt;
&lt;br /&gt;
  public LinkedQueue();&lt;br /&gt;
  public int size();&lt;br /&gt;
  public boolean isEmpty();&lt;br /&gt;
  public void enqueue(Object item);&lt;br /&gt;
  public Object dequeue() throws QueueEmptyException;&lt;br /&gt;
  public Object front() throws QueueEmptyException;&lt;br /&gt;
  public String toString();&lt;br /&gt;
  public Object nth(int n);&lt;br /&gt;
  public void append(LinkedQueue q);&lt;br /&gt;
&lt;br /&gt;
The second-last method, nth(), returns item n in the queue (with the assumption&lt;br /&gt;
that items are numbered from 1) without removing it.  The last method,&lt;br /&gt;
append(), concatenates the contents of q onto the end of this queue (in&lt;br /&gt;
constant time), and leaves q empty.&lt;br /&gt;
&lt;br /&gt;
You will implement mergesort and quicksort in the file ListSorts.java.  In&lt;br /&gt;
Parts I and II below, assume that the input LinkedQueue (to be sorted) contains&lt;br /&gt;
only Comparable items, so that casting items to Comparable is safe.  All&lt;br /&gt;
comparisons should be done using the compareTo method.  Your code should be&lt;br /&gt;
work with any Comparable objects, not just the Integer objects used by the test&lt;br /&gt;
code.  (In other words, do not cast queue items to Integers.)&lt;br /&gt;
&lt;br /&gt;
The dequeue() and front() methods can throw QueueEmptyExceptions; make sure&lt;br /&gt;
that these exceptions are always caught.  (If your code is bug-free, such an&lt;br /&gt;
exception will never occur, so handle caught exceptions by simply printing an&lt;br /&gt;
error message.)  Do not add a &amp;quot;throws&amp;quot; clause to any method prototype that&lt;br /&gt;
doesn't already have one.&lt;br /&gt;
&lt;br /&gt;
===Part I  (4 points)===&lt;br /&gt;
Implement mergesort on LinkedQueues using the following three steps.&lt;br /&gt;
&lt;br /&gt;
(1)  Write a method called makeQueueOfQueues() that takes a LinkedQueue as&lt;br /&gt;
input and returns a queue of queues in which each queue contains one item from&lt;br /&gt;
the input queue.  For example, if the input queue is [ 3 5 2 ], this method&lt;br /&gt;
should return the queue [ [ 3 ] [ 5 ] [ 2 ] ].&lt;br /&gt;
&lt;br /&gt;
  public static LinkedQueue makeQueueOfQueues(LinkedQueue q);&lt;br /&gt;
&lt;br /&gt;
(2)  Write a method called mergeSortedQueues() that merges two sorted queues&lt;br /&gt;
into one.  See the comments in ListSorts.java for details.&lt;br /&gt;
&lt;br /&gt;
  public static LinkedQueue mergeSortedQueues(LinkedQueue q1, LinkedQueue q2);&lt;br /&gt;
&lt;br /&gt;
(3)  Implement mergeSort(), which sorts a LinkedQueue q as follows.  First,&lt;br /&gt;
convert q into a queue of queues using makeQueueOfQueues().  Repeatedly do the&lt;br /&gt;
following:  remove two items from the queue of queues, merge them using&lt;br /&gt;
mergeSortedQueues(), and enqueue the resulting queue in the queue of queues.&lt;br /&gt;
When there is only one queue left on the queue of queues, move its items back&lt;br /&gt;
to q (preferably using the fast append() method).&lt;br /&gt;
&lt;br /&gt;
  public static void mergeSort(LinkedQueue q);&lt;br /&gt;
&lt;br /&gt;
===Part II  (4 points)===&lt;br /&gt;
Implement quicksort on LinkedQueues using the following two steps.&lt;br /&gt;
&lt;br /&gt;
(1)  Implement a partition() method that partitions a queue into three separate&lt;br /&gt;
queues containing items less than, equal to, or greater than a pivot item.&lt;br /&gt;
Again, see the comments for details.&lt;br /&gt;
&lt;br /&gt;
  public static void partition(LinkedQueue qIn, Comparable pivot,&lt;br /&gt;
                               LinkedQueue qSmall, LinkedQueue qEquals,&lt;br /&gt;
                               LinkedQueue qLarge);&lt;br /&gt;
&lt;br /&gt;
(2)  Implement quickSort(), which sorts a LinkedQueue q as follows.  Choose a&lt;br /&gt;
random integer between 1 and q.size().  Find the corresponding item using the&lt;br /&gt;
nth() method, and use the item as the pivot in a call to partition().&lt;br /&gt;
Recursively quickSort() the smaller and larger partitions, and then put all of&lt;br /&gt;
the items back into q in sorted order by using the append() method.&lt;br /&gt;
&lt;br /&gt;
  public static void quickSort(LinkedQueue q);&lt;br /&gt;
&lt;br /&gt;
There is test code for both mergesort and quicksort; run &amp;quot;java ListSorts&amp;quot;.&lt;br /&gt;
The test code generates different random arrays each time you run it.&lt;br /&gt;
&lt;br /&gt;
===Part III  (1 point)===&lt;br /&gt;
Uncomment the timing code in the main() method, then run your sorting routines&lt;br /&gt;
on larger lists.  By changing the SORTSIZE constant, you may change the size of&lt;br /&gt;
the queues you sort.  What are the running times (in milliseconds) for sorting&lt;br /&gt;
lists of sizes 10, 100, 1000, and 10,000?  Also try 100,000 if your code can do&lt;br /&gt;
it.  Put your answers in the GRADER file.&lt;br /&gt;
&lt;br /&gt;
===Part IV  (1 point)===&lt;br /&gt;
A sort is ''stable'' if items with equal keys always come out in the same order&lt;br /&gt;
they went in.  If you sort the database records [ 3:hex 7:boo 3:spa ] by&lt;br /&gt;
number, and the output is [ 3:spa 3:hex 7:boo ], then the sort is not stable&lt;br /&gt;
because two records with the same key (3) traded places.&lt;br /&gt;
&lt;br /&gt;
Is your mergesort always stable?  Explain why or why not.&lt;br /&gt;
Is your quicksort always stable?  Explain why or why not.&lt;br /&gt;
&lt;br /&gt;
Give a sentence or two in your explanations that describe where in your code or&lt;br /&gt;
in the data structures the stability of the sort is decided--that is, whether&lt;br /&gt;
or not equal items are kept in their original order at critical parts of the&lt;br /&gt;
sorting process.  Put your answers in the [[/GRADER|]] file.&lt;/div&gt;</summary>
		<author><name>Lensovet</name></author>
		
	</entry>
</feed>