Difference between revisions of "CSC212 Lab 12 2014"

From dftwiki3
Jump to: navigation, search
(Lab Problem #2: HeapSort)
(Lab Problem #0: Observing Quicksort at Work)
 
(14 intermediate revisions by the same user not shown)
Line 8: Line 8:
 
</bluebox>
 
</bluebox>
 
<br />
 
<br />
 +
=Something Fun=
 +
<br />
 +
<center><videoflash>es2T6KY45cA</videoflash></center>
 +
<br />
 +
<br />
 +
<center><videoflash>kPRA0W1kECg</videoflash></center>
 +
<br />
 +
<br />
 +
 +
=Lab Problem #0: Observing Quicksort at Work=
 +
<br />
 +
* Point your '''Firefox''' or '''Safari''' browser to this URL: [http://cs.smith.edu/~dthiebaut/java/sort/Welcome.html http://cs.smith.edu/~dthiebaut/java/sort/Welcome.html].  Chrome won't work with 64-bit Java Applets, so no need to try it...
 +
* You may need to '''resize''' the window or shrink/expand the applet to see the '''Start''' Button.
 +
<br />
 +
==Standard Quicksort, Random Array==
 +
<br />
 +
* Select '''Standard Quicksort''' for the Sorting Algorithm
 +
* Select '''Random''' for the Array Type
 +
* Select '''Swap Square''' for the View
 +
* Delay of 25
 +
* '''Start!'''
 +
* <font color="magenta">Notice how partition works.  Whatever group of bars is red represent the partition that is being split into 2 around the pivot.</font>
 +
* Don't hesitate to run the experiment a few times until you "get it".
 +
<br />
 +
==Standard Quicksort, Array in Increasing Order==
 +
<br />
 +
* Click '''Stop''' so that you can start a new experiment.
 +
* Select '''Standard Quicksort''' for the Sorting Algorithm
 +
* Select '''Sorted Incr. Order''' for the Array Type
 +
* Select '''Swap Square''' for the View
 +
* Delay of 25
 +
* '''Start!'''
 +
* <font color="magenta">Notice how the partition is of size ''N'' first, then ''N-1'', then ''N-2'', etc.</font>
 +
<br />
 +
==Standard Quicksort, Array in Decreasing Order==
 +
<br />
 +
* Click '''Stop''' so that you can start a new experiment.
 +
* Select '''Standard Quicksort''' for the Sorting Algorithm
 +
* Select '''Sorted Decr. Order''' for the Array Type
 +
* Select '''Swap Square''' for the View
 +
* Delay of 25
 +
* '''Start!'''
 +
* <font color="magenta">Notice how the partition is of size ''N'' first, then ''N-2'', then ''N-4'', etc.</font>
 +
<br />
 +
==Quicksort with a Random Pivot==
 +
<br />
 +
* Repeat the same experiments above, (random array, sorted in increasing order, sorted in decreasing order), but pick "Quicksort with Random Pivot" as the algorithm.
 +
* Does the random pivot help with the random array?
 +
<br />
 +
==Quicksort with Median of Three Pivot==
 +
<br />
 +
* Repeat the same experiments above, (random array, sorted in increasing order, sorted in decreasing order), but pick "Quicksort with Median of Three Pivot" as the algorithm.  To refresh your memory, with the median of three, partition takes the median of the first, middle, and last number in the slice of the array it has to work on, as the pivot.
 +
* Is this method more efficient than the random pivot for sorted arrays?
 +
<br />
 +
==Quicksort with Tail Recursion Truncated==
 +
<br />
 +
* Same experiments again, but this time pick the "Quicksort with Tail Recursion Truncated" algorithm.  Try to answer the following questions to yourself, as a way of understanding how tail recursion works for Quicksort.  Nothing to submit anywhere...
 +
<br />
 +
;Question 1:
 +
:Can you see the tail recursion being truncated?  Which sorting method is used to sort the small partitions?
 +
;Question 2:
 +
:Is the O(N^2) sorting routine called on all the small partitions, separately, or is it called on the whole array?
 +
;Question 3:
 +
:Can you explain the two areas identified below?
 +
<br />
 +
<center>[[Image:QuicksortTailRecursionCut.png|400px]]</center>
 +
;Question 4:
 +
:Is the Quicksort algorithm cutting the tail recursion, also using a random pivot or not?
 +
<br />
 +
<br >
 +
 
=Lab Problem #1: Recursion-Stack Depth=
 
=Lab Problem #1: Recursion-Stack Depth=
 
<br />
 
<br />
 
* Create a class containing the code for [[Quicksort.java| Quicksort]].
 
* Create a class containing the code for [[Quicksort.java| Quicksort]].
* Run it on arrays of size 10, 200, 500, 1000, 5000, 10000, 50000, 100000.  Keep on increasing the size until something happens...
+
* Study the main function carefully before running the program.  Make sure you understand how it operates.
 +
* If you are using Eclipse, remember that command-line arguments are set in the "Run Configurations" panel, as explained in [[CSC212_Homework_5_2014#Problem_.231| Homework 5, Problem 1]].
 +
* Run it on '''sorted''' arrays of size 10, 200, 500, 1000, 5000, 10000, 50000, 100000.  Keep on increasing the size until something happens...
 
* You probably ran into an exception...  Make sure you read what the exception is...  Can you figure it out?
 
* You probably ran into an exception...  Make sure you read what the exception is...  Can you figure it out?
 
* Read the documentation of the -XSS command line on this [https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html page].
 
* Read the documentation of the -XSS command line on this [https://docs.oracle.com/cd/E13150_01/jrockit_jvm/jrockit/jrdocs/refman/optionX.html page].
 
* Apply it to your program
 
* Apply it to your program
::* If you are using Eclipse, click on the '''Run''' top menu-option, then '''Run Configurations''', then on the '''Arguments''' tab, and in the '''VM arguments''' window, enter something like -Xss64m (you'll have to do go through some trials and errors to find the right value.
+
::* If you are using Eclipse, click on the '''Run''' top menu-option, then '''Run Configurations''', then on the '''Arguments''' tab, and in the '''VM arguments''' window, enter something like -Xss4m (you'll have to do go through some trials and errors to find the right value.
:: * If you are running your Java code on beowulf, simply add the option on the command line:
+
::* If you are running your Java code on beowulf, simply add the option on the command line for the Java Virtual Machine (in this case, a request for 64 Megabytes of stack size):
+
 
    java -Xss64m QuickSort 1000000 random
+
:::<br /><tt>java -Xss64m QuickSort 1000000 random</tt><br />
 
   
 
   
 +
 
* Make sure you have define a large enough stack to sort at least a million ints.
 
* Make sure you have define a large enough stack to sort at least a million ints.
  
 
<br />
 
<br />
 +
 
=Lab Problem #2: HeapSort=
 
=Lab Problem #2: HeapSort=
 
[[Image:Heap.png|right|150px]]
 
[[Image:Heap.png|right|150px]]
Line 31: Line 106:
 
<br />
 
<br />
 
::<source lang="java">
 
::<source lang="java">
import java.util.PriorityQueue;
+
        import java.util.PriorityQueue;
 
 
 
public static void TestPrioQueues() {
 
public static void TestPrioQueues() {
Line 44: Line 119:
 
System.out.println( heap.poll() );
 
System.out.println( heap.poll() );
 
}
 
}
}
+
 
 
</source>
 
</source>
 
<br />
 
<br />
Line 67: Line 142:
 
[[Image:SlowSnail.jpg|right|150px]]
 
[[Image:SlowSnail.jpg|right|150px]]
 
<br />
 
<br />
* You should have gone through Problem #2 and modified  [[Quicksort.java| QuickSort]] so that you can measure its execution time.
+
* You will be modifying the [[Quicksort.java| QuickSort]] program of Problem #2.  You should have  modified  it so that you can measure its execution time.
* Name your file '''Lab12_3.java''', and your class '''Lab12_3''' (you will need to submit it to Moodle).
+
* Rename your file '''Lab12_3.java''', and rename your class '''Lab12_3''' (you will need to submit under that name  to Moodle).
* Run it on '''sorted''' arrays of varying sizes until you find an array size that requires between 1 and 10 seconds of execution time.
+
* Run it on '''sorted''' arrays of varying sizes (by providing different command line arguments) until you find an array size that requires between 1 and 10 seconds of execution time.
* Pick one of the methods we saw in class for allowing Quicksort to avoid worst case conditions, and implement it.  Modify your code.  
+
* Pick one of the methods you have observed in Section 0 of this lab, that makes Quicksort avoid worst case conditions, and implement it.  Modify the code of Lab12_3.java.
 
* Verify that you have seriously improved your Quicksort function with this mod.
 
* Verify that you have seriously improved your Quicksort function with this mod.
 
<br />
 
<br />
Line 76: Line 151:
 
<br />
 
<br />
 
* Submit your program to Moodle.  Your program will be run against the solution program on a sorted array.  The grade is proportional to how close your execution time comes to the execution time of the solution program.
 
* Submit your program to Moodle.  Your program will be run against the solution program on a sorted array.  The grade is proportional to how close your execution time comes to the execution time of the solution program.
 +
* Note: your program will be given the size of the array to sort, and whether the array is random, sorted in increasing order, or sorted in decreasing order, on the command line.  Make sure your program gets N and the array type from the args array in main().
 
<br />
 
<br />
 
<br />
 
<br />

Latest revision as of 09:46, 5 April 2016

--D. Thiebaut (talk) 19:37, 9 November 2014 (EST)




This lab deals with Sorting algorithms, HeapSort, QuickSort, and methods used to avoid worst-case conditions.


Something Fun






Lab Problem #0: Observing Quicksort at Work



Standard Quicksort, Random Array


  • Select Standard Quicksort for the Sorting Algorithm
  • Select Random for the Array Type
  • Select Swap Square for the View
  • Delay of 25
  • Start!
  • Notice how partition works. Whatever group of bars is red represent the partition that is being split into 2 around the pivot.
  • Don't hesitate to run the experiment a few times until you "get it".


Standard Quicksort, Array in Increasing Order


  • Click Stop so that you can start a new experiment.
  • Select Standard Quicksort for the Sorting Algorithm
  • Select Sorted Incr. Order for the Array Type
  • Select Swap Square for the View
  • Delay of 25
  • Start!
  • Notice how the partition is of size N first, then N-1, then N-2, etc.


Standard Quicksort, Array in Decreasing Order


  • Click Stop so that you can start a new experiment.
  • Select Standard Quicksort for the Sorting Algorithm
  • Select Sorted Decr. Order for the Array Type
  • Select Swap Square for the View
  • Delay of 25
  • Start!
  • Notice how the partition is of size N first, then N-2, then N-4, etc.


Quicksort with a Random Pivot


  • Repeat the same experiments above, (random array, sorted in increasing order, sorted in decreasing order), but pick "Quicksort with Random Pivot" as the algorithm.
  • Does the random pivot help with the random array?


Quicksort with Median of Three Pivot


  • Repeat the same experiments above, (random array, sorted in increasing order, sorted in decreasing order), but pick "Quicksort with Median of Three Pivot" as the algorithm. To refresh your memory, with the median of three, partition takes the median of the first, middle, and last number in the slice of the array it has to work on, as the pivot.
  • Is this method more efficient than the random pivot for sorted arrays?


Quicksort with Tail Recursion Truncated


  • Same experiments again, but this time pick the "Quicksort with Tail Recursion Truncated" algorithm. Try to answer the following questions to yourself, as a way of understanding how tail recursion works for Quicksort. Nothing to submit anywhere...


Question 1
Can you see the tail recursion being truncated? Which sorting method is used to sort the small partitions?
Question 2
Is the O(N^2) sorting routine called on all the small partitions, separately, or is it called on the whole array?
Question 3
Can you explain the two areas identified below?


QuicksortTailRecursionCut.png
Question 4
Is the Quicksort algorithm cutting the tail recursion, also using a random pivot or not?



Lab Problem #1: Recursion-Stack Depth


  • Create a class containing the code for Quicksort.
  • Study the main function carefully before running the program. Make sure you understand how it operates.
  • If you are using Eclipse, remember that command-line arguments are set in the "Run Configurations" panel, as explained in Homework 5, Problem 1.
  • Run it on sorted arrays of size 10, 200, 500, 1000, 5000, 10000, 50000, 100000. Keep on increasing the size until something happens...
  • You probably ran into an exception... Make sure you read what the exception is... Can you figure it out?
  • Read the documentation of the -XSS command line on this page.
  • Apply it to your program
  • If you are using Eclipse, click on the Run top menu-option, then Run Configurations, then on the Arguments tab, and in the VM arguments window, enter something like -Xss4m (you'll have to do go through some trials and errors to find the right value.
  • If you are running your Java code on beowulf, simply add the option on the command line for the Java Virtual Machine (in this case, a request for 64 Megabytes of stack size):

java -Xss64m QuickSort 1000000 random


  • Make sure you have define a large enough stack to sort at least a million ints.


Lab Problem #2: HeapSort

Heap.png


  • Java provides heap data structures, but calls them PriorityQueues.
  • Instead of keeping the largest element at the top of the heap, PriorityQueues keep the smallest element at the top.
  • Try the example below to see how to use a PriorityQueue


        import java.util.PriorityQueue;
	
	public static void TestPrioQueues() {

		PriorityQueue<Integer> heap = new PriorityQueue<Integer>();

		heap.add( 1 );
		heap.add( 20 );
		heap.add( 5 );
		heap.add( 100 );
		while ( ! heap.isEmpty() )
			System.out.println( heap.poll() );
	}


Question 1
Using some of the code/functions from this page, create a function called heapsort( int[] A ) that will use a priority queue to sort the array of ints A.


Question 2
Using the code snippet below, measure the execution times of QuickSort and of your HeapSort function. Figure out which is regularly faster on arrays of varying sizes.


        long start = System.nanoTime();
        quicksort(A, 0, A.length - 1);
        long end   = System.nanoTime();
        System.out.println( String.format(   
                                "quickSort( %d ) takes %1.3f msec", N,
                               (end-start)/1000000.0f ) );


Lab Problem #3: Speeding Up QuickSort

SlowSnail.jpg


  • You will be modifying the QuickSort program of Problem #2. You should have modified it so that you can measure its execution time.
  • Rename your file Lab12_3.java, and rename your class Lab12_3 (you will need to submit under that name to Moodle).
  • Run it on sorted arrays of varying sizes (by providing different command line arguments) until you find an array size that requires between 1 and 10 seconds of execution time.
  • Pick one of the methods you have observed in Section 0 of this lab, that makes Quicksort avoid worst case conditions, and implement it. Modify the code of Lab12_3.java.
  • Verify that you have seriously improved your Quicksort function with this mod.


Moodle Submission


  • Submit your program to Moodle. Your program will be run against the solution program on a sorted array. The grade is proportional to how close your execution time comes to the execution time of the solution program.
  • Note: your program will be given the size of the array to sort, and whether the array is random, sorted in increasing order, or sorted in decreasing order, on the command line. Make sure your program gets N and the array type from the args array in main().