Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing...

19
Computing and the Web Computing and the Web Algorithmic Thinking Algorithmic Thinking

description

Understanding a Specific Process n Putting items in order –Sorting –Alphabetizing n Methods –Compare and Swap –Form Multiple Piles –Divide and Conquer

Transcript of Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing...

Page 1: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Computing and the WebComputing and the WebAlgorithmic ThinkingAlgorithmic Thinking

Page 2: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

OverviewOverview Understanding a specific processUnderstanding a specific process Developing an algorithmDeveloping an algorithm Applying the algorithm to computer environmentApplying the algorithm to computer environment Implementing the algorithmImplementing the algorithm Different Language ChoicesDifferent Language Choices The Algorithm in ActionThe Algorithm in Action

Page 3: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Understanding a Specific Understanding a Specific ProcessProcess Putting items in orderPutting items in order

– Sorting Sorting – AlphabetizingAlphabetizing

MethodsMethods– Compare and SwapCompare and Swap– Form Multiple PilesForm Multiple Piles– Divide and ConquerDivide and Conquer

Page 4: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Developing An AlgorithmDeveloping An Algorithm A simplified approachA simplified approach Given index cards with a number on Given index cards with a number on

eacheach– A small number of cards is easy to handleA small number of cards is easy to handle– A large number of cards is awkwardA large number of cards is awkward– You can’t see all of the cards at the same You can’t see all of the cards at the same

timetime We need a methodical way of dealing We need a methodical way of dealing

with any number of cardswith any number of cards

Page 5: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Developing An AlgorithmDeveloping An Algorithm Given only two hands and a pile of cards:Given only two hands and a pile of cards:

– Pass through the pile holding out the highest Pass through the pile holding out the highest numbernumber

– Repeat the process over and over againRepeat the process over and over again A common name for this is a “Bubble A common name for this is a “Bubble

Sort”Sort” Doesn’t require a complex algorithmDoesn’t require a complex algorithm

Page 6: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

The Computer EnvironmentThe Computer Environment The computer memory can hold the The computer memory can hold the

index cards (rather their contents)index cards (rather their contents) We can set aside memory and give it a We can set aside memory and give it a

namename We can reference memory and We can reference memory and

manipulate the contents of itmanipulate the contents of it

Page 7: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

The Computer EnvironmentThe Computer Environment When we reference the name we mean When we reference the name we mean

all of the related memory locationsall of the related memory locations Each name must be uniqueEach name must be unique We can define how much memory is set We can define how much memory is set

aside for each nameaside for each name We call the names variablesWe call the names variables

Page 8: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

The Computer EnvironmentThe Computer Environment We can create arrays of similar itemsWe can create arrays of similar items Items can be referenced by their Items can be referenced by their

“number”“number” Array elements can be referenced by Array elements can be referenced by

their name and element numbertheir name and element number– Item(3) = 27Item(3) = 27

Page 9: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

The Computer EnvironmentThe Computer Environment We can use another variable to We can use another variable to

reference an element in an arrayreference an element in an array– j = 5j = 5– Item(j) = 27Item(j) = 27

The variable j is a “pointer”The variable j is a “pointer”

Page 10: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Implementing the algorithmImplementing the algorithm The index cards can be thought of as an The index cards can be thought of as an

arrayarray Your hands function like a pointerYour hands function like a pointer The algorithm was to compare and swap The algorithm was to compare and swap

as we work our way through the pileas we work our way through the pile

Page 11: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Implementing the algorithmImplementing the algorithm Cards = array(1 to 18)Cards = array(1 to 18) Current = 1Current = 1 Next = Current + 1Next = Current + 1 Compare Cards(Current) and Compare Cards(Current) and

Cards(Next)Cards(Next)– Swap them if they aren’t in orderSwap them if they aren’t in order

Increment Current and NextIncrement Current and Next

Page 12: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Implementing the algorithmImplementing the algorithmDimension Cards(18)Dimension Cards(18)Current = 1Current = 1Do count = 1 to 18Do count = 1 to 18 Next = Current + 1Next = Current + 1 If (Cards(Current) > Cards(Next)) Then DoIf (Cards(Current) > Cards(Next)) Then Do Temp = Cards(Current)Temp = Cards(Current) Cards(Current) = Cards(Next)Cards(Current) = Cards(Next) Cards(Next) = TempCards(Next) = Temp EndEnd Current = Current + 1Current = Current + 1EndEnd

Page 13: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Implementing the algorithmImplementing the algorithmDimension Cards(18)Dimension Cards(18)Current = 1Current = 1Do again = 1 to 18Do again = 1 to 18 Do count = 1 to 18Do count = 1 to 18 Next = Current + 1Next = Current + 1 If (Cards(Current) > Cards(Next)) Then DoIf (Cards(Current) > Cards(Next)) Then Do Temp = Cards(Current)Temp = Cards(Current) Cards(Current) = Cards(Next)Cards(Current) = Cards(Next) Cards(Next) = TempCards(Next) = Temp EndEnd Current = Current + 1Current = Current + 1 EndEndEndEnd

Page 14: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Different Language ChoicesDifferent Language Choices The “C” languageThe “C” language

/* bubble sort the array */ /* bubble sort the array */ for (x=0; x < MAX-1; x++) for (x=0; x < MAX-1; x++) for (y=0; y < MAX-x-1; y++) for (y=0; y < MAX-x-1; y++) if (a[y] > a[y+1]) if (a[y] > a[y+1]) { { t=a[y]; t=a[y]; a[y]=a[y+1]; a[y]=a[y+1]; a[y+1]=t; a[y+1]=t; } } /* print sorted array */ /* print sorted array */ printf("--------------------\n"); printf("--------------------\n"); for (i=0; i < MAX; i++) for (i=0; i < MAX; i++) printf("%d\n",a[i]); printf("%d\n",a[i]);

Page 15: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Different Language ChoicesDifferent Language Choices The “Fortran” The “Fortran”

languagelanguage DIMENSION NUMS(15)

CC SORT THE ARRAY USING BUBBLE SORT. 40 N = N - 1 70 ISCAN = 1 IOK = 1 ISTOP = N IF(ISTART - ISTOP) 50, 110, 110 50 IF(NUMS(ISCAN) - NUMS(ISCAN+1)) 90,90,60CC SWAP 60 J = NUMS(ISCAN) NUMS(ISCAN) = NUMS(ISCAN+1) NUMS(ISCAN+1) = J IOK = 0

C

C TIME TO ISTART OVER?

90 IF(ISCAN - (ISTOP - 1)) 80,100,100

C

C NEXT PAIR

80 ISCAN = ISCAN + 1

GOTO 50

C

C NEXT ISCAN

100 IF(IOK) 105,105,110

105 ISTOP = ISTOP - 1

GOTO 70

C

C PRINT

110 DO 120 I=1, N

120 PRINT 130, NUMS(I)

130 FORMAT(I10)

STOP

END

Page 16: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Different Language ChoicesDifferent Language Choices The “Java” languageThe “Java” languageThis version of bubble sort makes a fixed number of passes (length of the array - 1).

Each inner loop is one shorter than the previous one. public static void bubbleSort2(int[] x) { boolean doMore = true; while (doMore) { doMore = false; // assume this is last pass over array for (int i=0; i<x.length-1; i++) { if (x[i] > x[i+1]) { // exchange elements int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; doMore = true; // after an exchange, must look again } } }}

Page 17: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Modified AlgorithmsModified AlgorithmsBubble Sort -- stop when no exchanges, shorter range each timeThis version of bubble sort combines ideas from the previous versions. It stops when

there are no more exchanges, and it also sorts a smaller range each time. public static void bubbleSort3(int[] x) { int n = x.length; boolean doMore = true; while (doMore) { n--; doMore = false; // assume this is our last pass over the array for (int i=0; i<n; i++) { if (x[i] > x[i+1]) { // exchange elements int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; doMore = true; // after an exchange, must look again } } }}//end method bubbleSort3

Page 18: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

Modified AlgorithmsModified AlgorithmsBubble Sort -- Sort only necessary rangeAfter a pass on bubble sort, it's only necessary to sort from just below the first exchange to just after the last exchange, because

everything that wasn't exchanged must be in the correct order. This version of bubble sort remembers the lowest and highest locations where there was an exchange, and sorts only that part of the array. Although it is a little more complicated, it is more efficient than the other bubble sorts.

public static void bubbleSort4(int[] x) { int newLowest = 0; // index of first comparison int newHighest = x.length-1; // index of last comparison while (newLowest < newHighest) { int highest = newHighest; int lowest = newLowest; newLowest = x.length; // start higher than any legal index for (int i=lowest; i<highest; i++) { if (x[i] > x[i+1]) { // exchange elements int temp = x[i]; x[i] = x[i+1]; x[i+1] = temp; if (i<newLowest) { newLowest = i-1; if (newLowest < 0) { newLowest = 0; } } else if (i>newHighest) { newHighest = i+1; } } } }}//end method bubbleSort4

Page 19: Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer.

The Algorithm in ActionThe Algorithm in Action

http://math.hws.edu/TMCM/java/xSortLab/

http://java.sun.com/applets/jdk/1.0/demo/SortDemo/example1.html