1 / 19

Computing and the Web

Computing and the Web. Algorithmic Thinking. Overview. Understanding a specific process Developing an algorithm Applying the algorithm to computer environment Implementing the algorithm Different Language Choices The Algorithm in Action. Understanding a Specific Process.

alika-dyer
Télécharger la présentation

Computing and the Web

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Computing and the Web Algorithmic Thinking

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

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

  4. Developing An Algorithm • A simplified approach • 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 • We need a methodical way of dealing with any number of cards

  5. Developing An Algorithm • 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 • A common name for this is a “Bubble Sort” • Doesn’t require a complex algorithm

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

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

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

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

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

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

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

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

  14. Different Language Choices • The “C” language /* bubble sort the array */ for (x=0; x < MAX-1; x++) for (y=0; y < MAX-x-1; y++) if (a[y] > a[y+1]) { t=a[y]; a[y]=a[y+1]; a[y+1]=t; } /* print sorted array */ printf("--------------------\n"); for (i=0; i < MAX; i++) printf("%d\n",a[i]);

  15. Different Language Choices • 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 • 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/SortDemo/example1.html

More Related