Lab 9
comp 125-609, Goldwasser
Tuesday, 2 November 1999
Purpose: Animating Some Sorting Methods
In today's lab, we will use graphics to animate the sorting routines
we discussed during lecture. Seeing the sorting methods animated in
such a way is a great tool in better understanding the differences
between the sorting methods.
Please begin by downloading a program I have written (3 files):
www.cs.luc.edu/~mhg/comp125/labs/animatesort.frm
www.cs.luc.edu/~mhg/comp125/labs/animatesort.frx
www.cs.luc.edu/~mhg/comp125/labs/animatesort.vbp
Do not modify the design of the form. You are only to modify the
code as described in the rest of this lab.
This program has all the code for implementing the three sorts we discussed:
insertion sort, selection sort, and bubble sort. However none of the
code for the graphics has been written. This will be up to you.
When running the program, the user is allowed to choose four different
settings:
If you immediately download the files and run the program, you will
not see anything happen as no graphics routines have been written. If
at any point during today's lab you want to interrupt a program which
is running, press cntr-break.
Your first task is to write code for the routine named
AnimateDrawScreen() which is used in the program. This
routine should give a pictoral view of an array of numbers in the
picture box picture1 on the form.
The data is stored in a global array which was created with a line
such as
Dim TheArray(1 to arraysize) As Single
where arraysize is also a global variable. The actual data
consists of floating point numbers between 0 and 1 which are chosen at
random.
You should draw the array as a bar graph with the following
conditions:
You may start by using the foreground color for drawing all the bars.
As a bonus, you can make a much nicer looking drawing by also choosing
the colors for each bar in a way that depends on the data value.
When you have successfully written this routine, you should be able to
run the program and see the sorts animated. The animation may flicker
a bit as the program runs though. The reason for this is that each
step of the animation proceeds by completely erasing and redrawing the
picture whenever the underlying array is modified. Although drawing
takes place relatively quickly, our eyes may still notice this
flickering effect.
In the second half of the lab we will attempt to remove this flickering
effect by being a bit more clever. As it happens, all three of these
sorts modify the array only by swapping two items at a time. Since we
know this, we can animate the algorithm more efficiently by not
redrawing the entire picture from scratch at each step. Instead of
clearing the screen, we will write an additional graphics routine
AnimateSwap() which animates a single swap.
In order to animate a swap, it is necessary to know which two items
have just been swapped, and so this routine is called with two
parameters i and j which denote the index location
of the two swapped items. NOTE: you are not to modify the values in
TheArray as this has already been done elsewhere in
the code. You are only supposed to modify your drawing.
In this routine you should do two things.
When you have successfully written this routine, run the program
again, this time selecting the "Smart Animation" option. You should
the sorts animated as before, but with a much "smoother" appearance.
This lab is due before leaving class today. When you have completed
both parts of the program, please call me over to your computer to run
your program.
Also, you may want to save your program for your own use, as these
animations are quite a good way to better understand the three sorting
methods we have learned!