Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Programming Project 2 SORTING Lecture 05, file P2 Due January.

Similar presentations


Presentation on theme: "1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Programming Project 2 SORTING Lecture 05, file P2 Due January."— Presentation transcript:

1 1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek 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 http://www.gvu.gatech.edu/~jarekJarek Rossignac Start with the sample P2 Access the applet at http://www.gvu.gatech.edu/~jarek/courses/1050/processing/P2/ http://www.gvu.gatech.edu/~jarek/courses/1050/processing/P2/ 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 http://www.gvu.gatech.edu/~jarekJarek 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 http://www.gvu.gatech.edu/~jarekJarek 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<H; i++) {T[i]=int(random(2*H)); }; // init with random numbers for (int i=0; i<H; i++) {AT[i]=T[i]; }; // copy to animation table println(); println(" *** unsorted list ***"); for (int i=0; i<H; i++) { println("T["+i+"]="+T[i]);}; bubbleSort(); // call to my naive sorting routine }; // end of setup

5 5 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac bubbleSort void bubbleSort() { // my simple sorting, replace by yours for (int j=H-1; j>0; j--) { for (int i=0; i<j; i++) { if (T[i]>T[i+1]) {doSwap(i,i+1);}; }; }; }; void doSwap(int s1, int s2) { int temp=T[s1]; T[s1]=T[s2]; T[s2]=temp; // performs the swap swaps[swapMax][0]=s1; // records the swap swaps[swapMax][1]=s2; swapMax++; };

6 6 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek 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 http://www.gvu.gatech.edu/~jarekJarek Rossignac advanceAnimation void advanceAnimation () { s1=swaps[swap][0]; s2=swaps[swap][1]; // entries to be swapped are retrieved from list of swaps if (w<mw) {w++;} // advance the wait counter if(w==mw) { // when done waiting t=float(c)/float(mc); // compute animation time in [0,1] c++; // advance animation counter if (c==mc) { // when animation done for this swap w=0; c=0; t=0; // reset time and counters int temp=AT[s1]; AT[s1]=AT[s2]; AT[s2]=temp; // do the actual swap swap++; // next swap if (swap==swapMax) {done=true; swap=swapMax;} // all swaps done };

8 8 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek 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<H; h++) { // go through the cells cellColor=myGreen; // default color for non-swapping cells is green ax=0; ay=0; // no displacement for non-swapping cells if (h==s1) {ay=int(t*r); ax=sa; cellColor=myRed; }; // if upper swap cell if (h==s2) {ay=-int(t*r); ax=-sa; cellColor=myBlue; }; // if lower swap cell drawCell(width/2-cw+ax,h*ch+ay,cw,ch,cellColor,AT[h]); }; }; void drawCell(int wp, int hp, int cwp, int chp, color cp, int vp) { fill(cp); rect(wp,hp,cwp,chp); // draws and fills a rectangular region fill(0); text(nf(vp,2),wp+cwp/10,hp+chp*0.8); }; // writes vp on the screen

9 9 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek 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 http://www.gvu.gatech.edu/~jarekJarek 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 http://www.gvu.gatech.edu/~jarekJarek 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


Download ppt "1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Programming Project 2 SORTING Lecture 05, file P2 Due January."

Similar presentations


Ads by Google