Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 101 A Survey of Computer Science

Similar presentations


Presentation on theme: "Computer Science 101 A Survey of Computer Science"— Presentation transcript:

1 Computer Science 101 A Survey of Computer Science
Searching

2 Searching Searching is one of the most common operations of computing
Search for student record based on id Search for “best” item based on some criteria Search for airline reservations Clearly we know all there is to know about searching

3 Or do we?

4 Or do we?

5 Or do we?

6 Or do we?

7 Or do we?

8 Sequential Search Algorithm
Given: List of values v1,v2,…,vn Target value, T Output: Information about T if in list and message if T is not in list Strategy: Use variables: i for current location in list Found to indicate whether T located or not Initialize i=1, Found=false Pass through the list sequentially

9 Sequential Search-Names/Phone No’s Given: Names N1,N2,…Nn Target NAME Phone No’s T1,T2,…,Tn Output: Phone number of NAME Note: We’ll use this as an example for sequential search, but if the names are in alphabetical order there is a much better algorithm.

10 Names/Phone No’s Algorithm Set Found to false Set i to 1 While not Found and i ≤ last position Do if Ni = NAME then Print Ti Set Found to true else Set i to i+1 end-of-loop if Found=false then Print ‘Sorry, not in directory’ Stop

11 Algorithm to Find Largest
Given: List of numbers A1,A2,…,An Output: Largest value in list and its position Strategy: Use variables: i for current location in list Largest for largest value found so far LargeLoc for position of largest found so far Initialize Largest=A1,LargeLoc=1,i=2 Pass through the list sequentially

12 Algorithm to Find Largest (cont)
Set Largest to A1 Set LargeLoc to 1 Set i to 2 While i  n do if Ai > Largest then Set Largest to Ai Set LargeLoc to i Set i to i+1 end-of-loop Print Largest, LargeLoc Stop

13 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n 8

14 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

15 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

16 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

17 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

18 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

19 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

20 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

21 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

22 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

23 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

24 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

25 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

26 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

27 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

28 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

29 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

30 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

31 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

32 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

33 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

34 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

35 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

36 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

37 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

38 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

39 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

40 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

41 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

42 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

43 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

44 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

45 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

46 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

47 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

48 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

49 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n

50 Example: 1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2
1. Set Largest to A1 2. Set LargeLoc to 1 3. Set i to 2 4. While i  n do 5. if Ai > Largest then Set Largest to Ai Set LargeLoc to i 8. Set i to i+1 9. end-of-loop 10. Print Largest, LargeLoc 11. Stop Largest LargeLoc i n OUTPUT: 15, 6

51 I guess we could call them Cow1, Cow2, Cow3, ...


Download ppt "Computer Science 101 A Survey of Computer Science"

Similar presentations


Ads by Google