Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 05: SORTING Section 2.1 Jarek Rossignac CS1050: Understanding.

Similar presentations


Presentation on theme: "1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 05: SORTING Section 2.1 Jarek Rossignac CS1050: Understanding."— Presentation transcript:

1 1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Lecture 05: SORTING Section 2.1 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 Lecture Objectives Translate a problem into a mathematical model Define a discrete structure for representing it Devise an algorithm for solving it Prove (?) correctness … Analyze complexity

3 3 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac What is an algorithm? Finite set of instructions for producing the desired result Can be formulated in English at a high level –Ambiguities in the problem stated must be resolved May assume a particular domain for which it is valid (should be made explicit) –Coordinates are positive integers in [0,2 10 –1] –Polygon has a single bounding loop Then translated into pseudo-code to remove ambiguities and guide implementation Usually assumes a particular representation of the input and output (should be made explicit) –Represent a polygon by ordered sets of vertices –A choice of representation does not dictate a data structure Linked list? Table?

4 4 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac English for “Finding Max” ALGORITHM Initialize Max to the first element Go through the others and update each time we find one that exceeds max AMBIGUITIES What are we reporting? Value (Max) or ID of element? What if there is more than one?

5 5 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Domain for “Finding Max” Things that can be compared with > –Numbers –Strings –Sets (inclusion) –Nodes in a tree (ancestor) What if there is no absolute order –Disjoint or intersecting sets

6 6 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Pseudo-code for “Finding Max” Input = set S Output = value of largest element of S element findMax(set S) { e = first element of S; max = value(e); for each element e of S { if (value(e)>max) {max=value(e); }; return(max); }

7 7 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Representation for “Finding max” Ordered set S that can be accessed using iterations on an identifier e of its elements –for each element e of S We will use an array –We could use a linked list

8 8 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Processing code for “Finding Max” int[] T = new int [6]; T[0]=4; T[1]=2; T[2]=5; T[3]=1; T[4]=5; T[5]=3; int m=T[0]; for (int e=0; e<6; e++) { if (T[e]>m) {m=T[e];}; }; println(m);

9 9 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Print all elements with max value Change the code (below) to –print max –print the IDs of all elements having max value int[] T = new int [6]; T[0]=4; T[1]=2; T[2]=5; T[3]=1; T[4]=5; T[5]=3; int m=T[0]; for (int e=0; e m) {m=T[e];};}; println(m);

10 10 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Find an element with a given value Initialize f to a dummy id Visit all the elements –Each time you find an element with desired value, update f

11 11 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Linear search (what does it report?) float[] T = new float [6]; float v=5; void setup() { size(300,300); T[0]=1.0; T[1]=2.0; T[2]=4.0; T[3]=4.0; T[4]=5.0; T[5]=6.0; }; void draw(){}; void mousePressed() { int f = linearSearch(v,0,6); if (f==-1) {println("No match");} else {println("found "+v+" in T["+f+"]");}; noLoop(); }; int linearSearch(float pv, int pl, int ph) { int e= -1; for (int i=pl; i<ph; i++) {if (T[i]==pv) {e=i;};}; return(e); };

12 12 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Change it to stop early Change the linear search code (below) to stop when it finds a match int linearSearch(float pv, int pl, int ph) { int e= -1; for (int i=pl; i<ph; i++) {if (T[i]==pv) {e=i;};}; return(e); };

13 13 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Use binary search Domain: Input is sorted (increasing order) Strategy: Recursively split domain in two Data structure: Array T[0]=1.0 T[1]=2.0 T[2]=4.0 T[3]=6.0 T[4]=7.0 T[5]=8.0 T[6]=8.0 T[7]=9.0 01234567 1.02.04.06.07.08.0 9.0 ij

14 14 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Example: Binary search for v=4.0 while (i<j) { m=(i+j)/2; if (v>T[m]) {i=m+1;} else { j=m;} if (T[i]==v) { return(i);} else { return(-1);}; 01234567 1.02.04.06.07.08.0 9.0 imj 01234567 1.02.04.06.07.08.0 9.0 imj 01234567 1.02.04.06.07.08.0 9.0 ij 01234567 1.02.04.06.07.08.0 9.0 i=j

15 15 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Write the code for Binary search ! float[] T = new float [6]; float v=4.0; void setup() { size(300,300); T[0]=1.0; T[1]=2.0; T[2]=4.0; T[3]=6.0; T[4]=7.0; T[5]=9.0; }; void draw(){}; void mousePressed() { int f = binarySearch(v,0,6); if (f==-1) {println(" No match");} else {println("found "+v+" in T["+f+"]"); }; noLoop(); }; int binarySearch(float pv, int pl, int ph) { int f= -1; ??? return(f); };

16 16 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Cost analysis Assume v is a random number in the set of n numbers Expected cost of linear search? Expected cost of binary search? Assume v is not in the set Expected cost of linear search? Expected cost of binary search?

17 17 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac What if the set is not sorted? If you expect to have to search it many times, it may pay off to sort it first. Assume that you will do s searches and half the time you will find the element. For what value of s does it pay off to sort if sorting takes s 2 steps?

18 18 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac How to sort? Many strategies for different objectives: simple implementation fast for random order fast if set is almost sorted require little working memory (footprint) fast for practical cases fast asymptotically, as the size of the set grows to infinity

19 19 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac What is bubble sort? traverse the array many times left-to-right compare the current element with the next one –swap them if they are out of order 372545169928 i 372545169928 i 327545169928 i 327545169928 i

20 20 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Example of bubble sort Unsorted array: 45 67 12 34 25 39 Effective array of size 6: 45 12 34 25 39 67 Effective array of size 5: 12 34 25 39 45 Effective array of size 4: 12 25 34 39 Effective array of size 3: 12 25 34 Effective array of size 2: 12 25 Effective array of size 1: 12 Sorted array: 12 25 34 39 45 67

21 21 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Describe what Bubble Sort does What do we know at the end of the first pass through all the elements? What do we know at the end of the k th pass? How many passes do we need? Where can we stop the second pass? Where can we stop the k th pass? Write the loops for Bubble Sort.

22 22 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Check Links to Bubble Sort Nice animation: http://www.ee.unb.ca/petersen/lib/java/bubblesort/ Other sites: http://www.scism.sbu.ac.uk/law/Section5/chap2/s5c2p13.html

23 23 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Bidirectional bubblesort: A variant of bubble sort that compares each adjacent pair of items in a list in turn, swapping them if necessary, and alternately passes through the list from the beginning to the end then from the end to the beginning. It stops when a pass does no swaps.bubble sortlist Note: Complexity is O(n 2 ) for arbitrary data, but approaches O(n) if the list is nearly in order at the beginning. Bidirectional bubble sort usually does better than bubble sort since at least one item is moved forward or backward to its place in the list with each pass. Bubble sort moves items forward into place, but can only move items backward one location each pass.O(n 2 ) http://www.nist.gov/dads/HTML/bidirectionalBubbleSort.html

24 24 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac (Backward) Insertion sort Starting with the second element, visit all elements e For each, bubble it forward swapping with the preceding element as long as it is smaller than the preceding element. It keeps the elements to the left of e sorted. When is is faster than bubble sort? http://www.ee.unb.ca/petersen/lib/java/insertionsort/

25 25 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Selection sort http://www.ee.unb.ca/petersen/lib/java/selectionsort/

26 26 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac HH HH LL LL DD DD Train sorting You have a table large enough to hold 3 decks of cards. Sort a deck D (facing up) using one hand, no memory Invent a sorting algorithm and write it using the moves DL, HD… that move the top of one deck to another other DD LL HH DH HDDL LD HL LH original deck sorted deck (when done)

27 27 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac What is a greedy algorithm? Optimization algorithm that at each steps selects the move that minimizes some cost function. Sometimes, selecting a different move would result in a better overall solution.

28 28 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Change-making algorithm Write a greedy change-making algorithm that uses the least number of coins to make n cents change using pennies, nickles, dimes, and quarters. Prove that it is optimal.

29 29 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Assigned Reading Section 2.1

30 30 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Assigned Exercises (for quiz) P129: 7 P130: 9, 37 P131: 42

31 31 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Assigned Project P2, due January 31 Described on the class web pages Inspired by exercises 5, 6, 9, 3 on page 211

32 32 Georgia Tech, IIC, GVU, 2006 MAGIC Lab http://www.gvu.gatech.edu/~jarekJarek Rossignac Midterm Tu Feb 7 Closed books Individual Mostly made of assigned exercises for each lecture and possible questions about projects 10% f the final grade


Download ppt "1 Georgia Tech, IIC, GVU, 2006 MAGIC Lab Rossignac Lecture 05: SORTING Section 2.1 Jarek Rossignac CS1050: Understanding."

Similar presentations


Ads by Google