Presentation is loading. Please wait.

Presentation is loading. Please wait.

Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn.

Similar presentations


Presentation on theme: "Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn."— Presentation transcript:

1 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn Brookshear

2 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-2 Iterative Structures Pretest loop: while (condition) do (loop body) Posttest loop: repeat (loop body) until(condition)

3 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-3 Figure 5.7 Components of repetitive control

4 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-4 Figure 5.8 The while loop structure

5 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-5 Figure 5.9 The repeat loop structure

6 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-6 Iterative Algorithms EXAMPLES : Sequential search Insertion sort Selection sort

7 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-7 Figure 5.6 The sequential search algorithm in pseudocode

8 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-8 Figure 5.10 Insertion sort

9 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-9 Figure 5.11 The insertion sort algorithm expressed in pseudocode

10 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Fibonacci : Iterative Last  0; Current  1; while(Current <100) do (print the value assigned to Current; Temp  Last; Last  Current; and Current  Last+Temp;) 5-10

11 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-11 Recursion The execution of a procedure leads to another execution of the procedure. Multiple activations of the procedure are formed, all but one of which are waiting for other activations to complete. Base case =degenerative case EXAMPLE: Binary search

12 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursive Example 1 // initial value of Current is 0 procedure MyWrite (Current) if(Current<3) then (print the value assigned to Current; Current  Current+1; Apply MyWrite to the value Current) 5-12

13 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Recursive Example 2 // input values are 0 and 1 procedure MysteryWrite (Last, Current) if(Current<3) then (print the value assigned to Current; Temp  Current+Last; Apply MysteryWrite to the values Current and Temp) 5-13

14 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-14 Figure 5.12 Applying our strategy to search a list for the entry John (Binary Search)

15 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-15 Figure 5.13 A first draft of the binary search technique

16 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-16 Figure 5.14 The binary search algorithm in pseudocode

17 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-17 Figure 5.15

18 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-18 Figure 5.16

19 Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 5-19 Figure 5.17


Download ppt "Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Algorithms Computer Science: An Overview Tenth Edition by J. Glenn."

Similar presentations


Ads by Google