Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University...

22
Fall 2001 (c)opyright Brent M. Ding le 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)

description

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?

Transcript of Fall 2001(c)opyright Brent M. Dingle 2001 Simple Sorting Brent M. Dingle Texas A&M University...

Page 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

Simple SortingBrent M. DingleTexas A&M UniversityChapter 10 – Section 1(and some from Mastering Turbo Pascal 5.5, 3rd Edition by Tom Swan)

Page 2: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

What is Sorting?Sorting things is putting them into some kind of order.

Page 3: 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.

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?

Page 4: 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.

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.

Page 5: 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.

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.

Page 6: 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.

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?

Page 7: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

ConsiderAssume 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.

Page 8: 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.

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.

Page 9: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

Consider QuestionHow would you perform this task?

Oh, and there is one restriction: You can only look at 2 cards at any

given time.

Page 10: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

Consider 2Now 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?

Page 11: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

Algorithm Data TypeAssume 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;

Page 12: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

NoticeShoebox is thus an array of type string[40] whose indexing starts at 1 and ends on 12.

Page 13: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

AlgorithmAssume 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.

Page 14: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

Algorithm – general ideaLook 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

Page 15: 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.

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 holeAnd everything becomes sorted (ordered)

Page 16: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

The AlgorithmSome variables we “know” must be

used, there may be others:shoebox = Variable Parameter of type array[1..NUM_HOLES] of

NAME_TYPEi, j = Local variables of type integer

Page 17: 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.

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 ]) thenExchange(shoebox[i], shoebox[j]);

End { for j } END { for i }END;

Page 18: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

Algorithm – finishing itNotice 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.

Page 19: 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.

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?

Page 20: 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.

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.

Page 21: 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.

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.

Page 22: 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.

Fall 2001 (c)opyright Brent M. Dingle 2001

End Simple Sorting