Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer."— Presentation transcript:

1 Computing and the Web Algorithmic Thinking

2 Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer environment n Implementing the algorithm n Different Language Choices n The Algorithm in Action

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

4 Developing An Algorithm n A simplified approach n Given index cards with a number on each –A small number of cards is easy to handle –A large number of cards is awkward –You can’t see all of the cards at the same time n We need a methodical way of dealing with any number of cards

5 Developing An Algorithm n Given only two hands and a pile of cards: –Pass through the pile holding out the highest number –Repeat the process over and over again n A common name for this is a “Bubble Sort” n Doesn’t require a complex algorithm

6 The Computer Environment n The computer memory can hold the index cards (rather their contents) n We can set aside memory and give it a name n We can reference memory and manipulate the contents of it

7 The Computer Environment n When we reference the name we mean all of the related memory locations n Each name must be unique n We can define how much memory is set aside for each name n We call the names variables

8 The Computer Environment n We can create arrays of similar items n Items can be referenced by their “number” n Array elements can be referenced by their name and element number –Item(3) = 27

9 The Computer Environment n We can use another variable to reference an element in an array –j = 5 –Item(j) = 27 n The variable j is a “pointer”

10 Implementing the algorithm n The index cards can be thought of as an array n Your hands function like a pointer n The algorithm was to compare and swap as we work our way through the pile

11 Implementing the algorithm n Cards = array(1 to 18) n Current = 1 n Next = Current + 1 n Compare Cards(Current) and Cards(Next) –Swap them if they aren’t in order n Increment Current and Next

12 Implementing the algorithm Dimension Cards(18) Current = 1 Do count = 1 to 18 Next = Current + 1 Next = Current + 1 If (Cards(Current) > Cards(Next)) Then Do If (Cards(Current) > Cards(Next)) Then Do Temp = Cards(Current) Temp = Cards(Current) Cards(Current) = Cards(Next) Cards(Current) = Cards(Next) Cards(Next) = Temp Cards(Next) = Temp End End Current = Current + 1 Current = Current + 1End

13 Implementing the algorithm Dimension Cards(18) Current = 1 Do again = 1 to 18 Do count = 1 to 18 Do count = 1 to 18 Next = Current + 1 Next = Current + 1 If (Cards(Current) > Cards(Next)) Then Do If (Cards(Current) > Cards(Next)) Then Do Temp = Cards(Current) Temp = Cards(Current) Cards(Current) = Cards(Next) Cards(Current) = Cards(Next) Cards(Next) = Temp Cards(Next) = Temp End End Current = Current + 1 Current = Current + 1 End EndEnd

14 Different Language Choices n The “C” language /* bubble sort the array */ 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 */ printf("--------------------\n"); for (i=0; i < MAX; i++) printf("%d\n",a[i]);

15 Different Language Choices n The “Fortran” language DIMENSION NUMS(15) C C 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,60 C C 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

16 Different Language Choices n The “Java” language This 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 }

17 Modified Algorithms Bubble Sort -- stop when no exchanges, shorter range each time This 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

18 Modified Algorithms Bubble Sort -- Sort only necessary range After 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

19 The Algorithm in Action http://math.hws.edu/TMCM/java/xSortLab/ http://java.sun.com/applets/jdk/1.0/demo/Sort Demo/example1.html http://java.sun.com/applets/jdk/1.0/demo/Sort Demo/example1.html


Download ppt "Computing and the Web Algorithmic Thinking. Overview n Understanding a specific process n Developing an algorithm n Applying the algorithm to computer."

Similar presentations


Ads by Google