Download presentation

Presentation is loading. Please wait.

Published byCristian Kicker Modified about 1 year ago

1
1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Programming Project 2 SORTING Lecture 05, file P2 Due January 31 before class as a link on your PPP Jarek Rossignac CS1050: Understanding and Constructing Proofs Spring 2006

2
2 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Start with the sample P2 Access the applet at Click in the window and hold mouse down to see animation It sorts random numbers and then animates the sorting

3
3 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Declarations PFont fontUsed; // name of font used for writing on the graphic screen int H=5; // number of entries in the table int T[] = new int[H]; // table (array). Will contain integers int AT[] = new int[H]; // copy of the table for animation int swaps[][] = new int[H*H+1][2]; // record swaps (pair of indices) color myRed = color(250,100,100); color myGreen = color(100,250,100); color myBlue = color(100,100,250); // my colors (R,G,B) in [0,255] int cw, ch; // cell width and height int c=0, mc=150; // animation counter and max int w=0, mw=50; // wait counter and max int swap=0; // index to swaps int swapMax=0; // counter of th enumber of swaps float t=0.0; // animation parameter int s1, s2; // indics of the current pair to be swapped

4
4 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Setup void setup() { // executed only once as initialization size(500, 500); // opens graphic window of 600x600 pixels. \ cw=int(35); // computes cell sizes leaving a margin, Cast to integer ch=int(0.9*height/H); // (height and width of window are keywords) fontUsed = loadFont("Times-Roman-25.vlw"); // this font must be loaded (MENU-BAR > TOOLS > CREATE FONTS) textFont(fontUsed, 20); // selects font and assigns size for (int i=0; i

5
5 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac bubbleSort void bubbleSort() { // my simple sorting, replace by yours for (int j=H-1; j>0; j--) { for (int i=0; i

6
6 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Draw // will be executed continuously to refresh the window many frames per second void draw() { background(200); // erases screen and set up a light grey background strokeWeight(2); // lines will be drawn thicker translate(width*0.05,height*0.05); // move the origin to leave a margin if (mousePressed) // moves animation by one step when mouse pressed {advanceAnimation(); }; ShowTable(); // Displays the partially sorted table with animation state if (done) {noLoop();}; // stops when done }; // end of draw

7
7 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac advanceAnimation void advanceAnimation () { s1=swaps[swap][0]; s2=swaps[swap][1]; // entries to be swapped are retrieved from list of swaps if (w

8
8 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac ShowTable void ShowTable() { // displays able animated int ax, ay; // displacements for the cels being swapped color cellColor = myGreen; // default color for cells float r=(s2-s1)*ch; // radius for circular animation int sa=int(r*sin(t*TWO_PI/2.0)/2.0); // horizontal displacement for (int h=0; h

9
9 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Implement 3 variants of bubblesort Bubble sort is described page 125 Add three alternatives to bubbleSort –A modified bubble sort algorithm that stops when no interchange is required (exercise 37 page 130) –Modified insertion sort If the next n th element is smaller than the one it follows, then bubble it up until it is in the correct place in the sorted list of the first n elements –A modified selection sort (presented at the bottom of page 130) Start with n=1 Assume that the first n–1 elements are sorted. Find the smallest of the remaining elements Swap it with the n th element Increment n

10
10 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Enhance the user interface Increase the size of the array. Let the user press a key for the following actions: “r”: to select a new random set of numbers “n”: to select a nearly sorted set of numbers “b”: to run my naïve bubblesort “e”: to run your modified bubblesort that stops early “i”: to run your modified insertion sort “s”: to run your modified selection sort When the mouse is in the window and pressed, run the animation of the algorithm that was run last (make sure that you write in the graphics window the total number of swaps).

11
11 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Report results Debug your program and make images for each combination Execute all 4 algorithms on several sets of random numbers and on several sets of nearly sorted numbers Watch the 4 algorithms and provide a qualitative characterization of the differences in their behavior for random and nearly sorted numbers Report statistics of the average number of swaps for each algorithm (separating random and nearly sorted sets). Post on your PPP: –images (before and after) with number of swaps for each algorithm on a random and on a nearly sorted set. –your qualitative characterization –statistics with conclusions –links to applet and source code

Similar presentations

© 2016 SlidePlayer.com Inc.

All rights reserved.

Ads by Google