Presentation is loading. Please wait.

Presentation is loading. Please wait.

Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal.

Similar presentations


Presentation on theme: "Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal."— Presentation transcript:

1 Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal 5.5, 3 rd Edition by Tom Swan)

2 Fall 2001(c)opyright Brent M. Dingle 2001 What is Sorting? Sorting things is putting them into some kind of order.

3 Fall 2001(c)opyright Brent M. Dingle 2001 Why Sort? Would a phone book be useful if it was not alphabetized? Is it better that it is? Do we sort on other things? What about student id numbers?

4 Fall 2001(c)opyright Brent M. Dingle 2001 What would we sort? Well, since we have just learned how to put many things in an array. And arrays certainly have an order to them. It would be nice if the order of the array reflected an order of what was contained in the array. e.g. if we have an array of names, it would be nice if the names were alphabetized. e.g. if we have an array of numbers, it might be nice if they were in sequential order.

5 Fall 2001(c)opyright Brent M. Dingle 2001 How do we sort? Notice that humans, if asked to sort things tend to lay them all out in front of them and pick them somehow. A computer can’t do that – it can’t see everything all at once. In fact it really can only see things about two at a time.

6 Fall 2001(c)opyright Brent M. Dingle 2001 How (cont) So if you were given a list of names (in no particular order) and you could only look at them two at a time how would you put them in order?

7 Fall 2001(c)opyright Brent M. Dingle 2001 Consider Assume somebody gave you a shoebox that had a bunch of ‘cubby hole’ divisions in it. Each hole was numbered (i.e. there was a first, a second,… a last). Each hole had an index card in it (that you can’t read unless you take the card out). On each card was a name.

8 Fall 2001(c)opyright Brent M. Dingle 2001 Consider (cont) Now you were asked to order the cards so that: the name in the first hole was alphabetically first among all the names the name in the second hole was alphabetically second among all the names and so on until all the cards were alphabetized based on which hole they were in.

9 Fall 2001(c)opyright Brent M. Dingle 2001 Consider Question How would you perform this task? Oh, and there is one restriction: You can only look at 2 cards at any given time.

10 Fall 2001(c)opyright Brent M. Dingle 2001 Consider 2 Now picture the same shoebox, But this time each index card has a number on it, And your task is to arrange the index cards from least to greatest. i.e. So the number of least value is in the first hole, and the number of greatest value is in the last hole, and all numbers are ordered in between the two. How would you perform this task?

11 Fall 2001(c)opyright Brent M. Dingle 2001 Algorithm Data Type Assume we have an array named: Shoebox declared as follows: CONST NUM_HOLES = 12; TYPE NAME_TYPE = string[40]; BOX_TYPE = array [1..NUM_HOLES] of NAME_TYPE; VAR shoebox : BOX_TYPE;

12 Fall 2001(c)opyright Brent M. Dingle 2001 Notice Shoebox is thus an array of type string[40] whose indexing starts at 1 and ends on 12.

13 Fall 2001(c)opyright Brent M. Dingle 2001 Algorithm Assume we have somehow ‘randomly’ placed names into the Shoebox array. We now want to construct an algorithm that will alphabetize the array. i.e. Make it so that shoebox[1] has the name which is alphabetically first, shoebox[2] has the name which is alphabetically second, and so on. You may wish to create such a shoebox and actually try to solve this problem before reading on.

14 Fall 2001(c)opyright Brent M. Dingle 2001 Algorithm – general idea Look at what is in the first hole, For all the holes 2 to NUM_HOLES  see if something ‘smaller’ can be found  if yes, then exchange it (switch it) for what is currently in the first hole  thus the first hole becomes ‘smaller’ in value  and will end up being the smallest of all values

15 Fall 2001(c)opyright Brent M. Dingle 2001 General idea (cont) Now look at what is in the second hole, For all the holes 3 to NUM_HOLES  see if something ‘smaller’ can be found  if yes, then exchange it (switch it) for what is currently in the first hole  thus the second hole becomes ‘smaller’ in value  and will end up being the smallest of all values (not counting what was placed in the first hole) Repeat this for the third, then fourth, then fifth, … up to the last hole And everything becomes sorted (ordered)

16 Fall 2001(c)opyright Brent M. Dingle 2001 The Algorithm Some variables we “know” must be used, there may be others: shoebox = Variable Parameter of type array[1..NUM_HOLES] of NAME_TYPE i, j = Local variables of type integer

17 Fall 2001(c)opyright Brent M. Dingle 2001 Algorithm (Pseudo-code) PROCEDURE SortThem(VAR shoebox : BOX_TYPE); VAR i, j : integer; BEGIN for i := 1 to NUM_HOLES do BEGIN for j := i + 1 to NUM_HOLES do Begin if (contents of shoebox[ j ] < shoebox [ i ]) then Exchange(shoebox[i], shoebox[j]); End { for j } END { for i } END;

18 Fall 2001(c)opyright Brent M. Dingle 2001 Algorithm – finishing it Notice we say exchange(shoebox[i], shoebox[j]) in the above algorithm, but we do not define it. This is an example of how we might break large tasks into smaller ones (Top Down Design) So while the above algorithm describes a method for sorting an array, it leaves the detail of how the exchange is done, left for later. We may also wonder exactly what is meant by (contents of shoebox[ i ] < contents of shoebox[ j ] ) – a detail to be worked out when we code the algorithm.

19 Fall 2001(c)opyright Brent M. Dingle 2001 Challenge (not graded) Write the pseudo-code for the Exchange procedure. What must it take as parameters? What local variables will it need? What are the preconditions and postconditions of calling it?

20 Fall 2001(c)opyright Brent M. Dingle 2001 Recommended (no grade) Actually implement (code) the above algorithm for alphabetizing an array of names. How would you alter the algorithm to sort names in ‘reverse’ alphabetical order (i.e. Z comes first and A comes last)? Write a separate program using a similar algorithm to sort an array of numbers in ascending order.

21 Fall 2001(c)opyright Brent M. Dingle 2001 Sample Program - challenge There should be a sample program available for download which demonstrates how to sort an array of strings based ONLY on their FIRST character. Try to alter it so it sorts on ALL characters.

22 Fall 2001(c)opyright Brent M. Dingle 2001 End Simple Sorting


Download ppt "Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University Chapter 10 – Section 1 (and some from Mastering Turbo Pascal."

Similar presentations


Ads by Google