Lab - Animating Quicksort

The files you need for this assignment can be downloaded here.


Contents:

  • Preface
  • Overview
  • Your palette
  • Existing variables
  • Your Tasks
  • Overview of java.awt.Graphics
  • Files you will need
  • Handing in your completed lab

  • Preface

    Often, a great way to really understand and learn an algorithm is to be able to see it in action! Our goal is to be able to animate the quick-sort algorithm by by using Java's "Abstract Windowing Toolkit" (Java.awt), and in particular their Graphics package (java.awt.Graphics).

    For motivation, you are free to see our demo, but please don't feel like you have to reproduce that exact animation. Be creative.

    This lab is designed to span over two consecutive lab meetings. In no way are we suggesting that you need to do any additional work during the intermediary week outside of lab. We simply feel that we can get a basic version of the lab done in one sitting, but can do even better coming back later with a second sitting. Please remember to save your code from the first week.

    Overview

    We are providing you with an Applet which contains the full code for quick-sort, but without any of the animation. Your job is to be the artist!

    To be more specific, the program sorts an array of integers with values from 0 to N-1, which are randomly shuffled before beginning. The code is modeled after the in-place quick-sort code from Section 10.3.1 of the text, however it has been specialized to sort an array of int's rather than a Sequence of Object's.

    The file QuickAnimation.java contains the quick-sort code. You do not need to modify the existing lines of this sorting method, however you will want to modify that files by inserting various animation commands which will allow the user to see what is going on as the sort progresses.

    Your palette

    When the user indicates that the sort should run, the applet initializes a new popup window for your use as a palette. Of course, the code we have provided you with does not yet draw on this palette.

    By default, if the array has N items, a window is created with width equal to N and height approximately 1.5 x N. The coordinate system is set up such that the origin (0,0) is located at the top-left corner of the screen. Our thought is that you can use an N x N square to show the values of the array, and that you can use the extra space below that square to add additional graphics to display the structure of the recursive calls. If the user would like to see a magnified view, there is a parameter mag >= 1 which scales the window to be larger.

    Existing variables

    The following variables are already defined and initialized for your routines:

    Your Tasks

    We will be making some very basic requirements for the minimal completion of the lab. The rest of it will be left open-ended for you to improve your animation as time and creativity permits.

    Here is our suggestion for getting going:

    Getting to this point will be deemed as success for week 1.

    There are lots of ways to improve the animation in order to better show the workings of quicksort. For the second week, you should go on to implement at least one of the following improvements:

    You can choose to animate in whatever style you wish. If you would like, you are free to see our demo, but please don't feel like you have to reproduce that exact animation. Be creative.

    Overview of java.awt.Graphics

    Your code contains a variable gimage which is an instantiation of the java.awt.Graphics class. You will need to use this variable to make most graphics call.

    The coordinate system for the Graphics object is defined so that the origin (0,0) is located at the top-left corner of the screen. Increasing the x-coordinate signifies moving farther to the right; increasing the y-coordinate signifies moving farther downward.

    The Graphics package is designed so that there is always a current pen color, though this choice of color can be changed as needed. At any point, you are allowed to draw basic shapes by specifying the relevant geometry in the coordinate system. When you draw on a graphics, you will be drawing on top of whatever you have previously drawn on that part of the coordinate system. For example, if you want black writing on a blue square, you will need to first draw the blue square and then draw the black text. Doing things in the reverse order will have the effect of painting the blue square overwriting the black text.

    Full documentation for the latest version of the Java.awt.Graphics package can be found online at,

    http://java.sun.com/j2se/1.3/docs/api/java/awt/Graphics.html

    Highlights of java.awt.Graphcis:

    We have provided a routine Redraw() which you must call every time you want your Graphics changes to be redisplayed on the screen. We have also placed a delay inside this routine in order to intentionally slow down your animation (based on a user parameter in the applet).

    Files you will need

    Although you will only need to modify one file (QuickAnimation.java), you will need to download several files we provide.

    Handing in your completed lab

    You do not have to submit any files electronically. You should instead make a printout of the modified file QuickAnimation.java and make sure your name is written clearly at the top of the file. Do this for each of the two weeks, to mark your relative progress.


    Last modified: 24 October 2002