Download presentation

Presentation is loading. Please wait.

Published byFredrick Rawles Modified over 2 years ago

1
Algorithmic Foundations COMP108 COMP108 Algorithmic Foundations Searching Prudence Wong http://www.csc.liv.ac.uk/~pwong/teaching/comp108/201415

2
Algorithmic Foundations COMP108 Searching Input: n numbers a 1, a 2, …, a n ; and a number X Output: determine if X is in the sequence or not Algorithm (Sequential search): 1. From i=1, compare X with a i one by one as long as i <= n. 2. Stop and report "Found!" when X = a i. 3. Repeat and report "Not Found!" when i > n. 2 (Searching)

3
Algorithmic Foundations COMP108 Sequential Search 12342975six numbers 7number X 12342975 7 12342975 7found! To find 7 3 (Searching)

4
Algorithmic Foundations COMP108 Sequential Search (2) 12342975 10 12342975 10not found! To find 10 4 (Searching)

5
Algorithmic Foundations COMP108 Sequential Search – Pseudo Code i = 1 while i <= n do begin if X == a[i] then report "Found!" and stop else i = i+1 end report "Not Found!" Challenge: Modify it to include stopping conditions in the while loop 5 (Searching)

6
Algorithmic Foundations COMP108 Sequential Search – Pseudo Code i = 1 found = false while i <= n && found==false do begin if X == a[i] then found = true else i = i+1 end if found==true then report "Found!" else report "Not Found!" 6 (Searching)

7
Algorithmic Foundations COMP108 Number of comparisons? i = 1 while i <= n do begin if X == a[i] then report "Found!" & stop else i = i+1 end report "Not Found!" Best case: X is 1st no. 1 comparison Worst case: X is last OR X is not found n comparisons 7 (Searching) How many comparisons this algorithm requires?

8
Algorithmic Foundations COMP108 Finding maximum / minimum... 2 nd max / min…

9
Algorithmic Foundations COMP108 9 (Searching) Finding max from n +ve numbers input: a[1], a[2],..., a[n] M = 0 i = 1 while (i <= n) do begin M = max(M, a[i]) i = i + 1 end output M What about minimum? For simplicity, we assume the function max(X, Y) returns the larger of X & Y

10
Algorithmic Foundations COMP108 10 (Searching) Finding min from n +ve numbers input: a[1], a[2],..., a[n] M = a[1] i = 1 while (i <= n) do begin M = min(M, a[i]) i = i + 1 end output M How many comparisons? n

11
Algorithmic Foundations COMP108 11 (Searching) Finding 1 st and 2 nd min input: a[1], a[2],..., a[n] M1 = min(a[1], a[2]) M2 = max(a[1], a[2]) i = 3 while (i <= n) do begin if (a[i] < M1) then M2 = M1, M1 = a[i] else if (a[i] < M2) then M2 = a[i] i = i + 1 end output M1, M2 Two variables: M1, M2 How to update M1, M2?

12
Algorithmic Foundations COMP108 12 (Searching) Finding location of minimum input: a[1], a[2],..., a[n] loc = 1 // location of the min number i = 2 while (i <= n) do begin if (a[i] < a[loc]) then loc = i i = i + 1 end output a[loc] (@ end of) Iteration loca[loc]i 1 2 3 4 Example a[]={50,30,40,20,10} 50 30 20 10 1 2 2 4 5 2 3 4 5 6

13
Algorithmic Foundations COMP108 Finding min using for-loop Rewrite the above while-loop into a for-loop input: a[1], a[2],..., a[n] loc = 1 for i = 1 to n do begin if (a[i] < a[loc]) then loc = i end output a[loc] 13 (Searching)

Similar presentations

OK

Program Design. The design process How do you go about writing a program? –It’s like many other things in life Understand the problem to be solved Develop.

Program Design. The design process How do you go about writing a program? –It’s like many other things in life Understand the problem to be solved Develop.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Download ppt on indus valley civilization government Ppt on diversity in living organisms class Ppt on nature and human figure Ppt online shopping Ppt on moving coil galvanometer Ppt on contact and noncontact force Ppt on addition and subtraction Download ppt on dot net Ppt on nonverbal communication body language Ppt on team building activities