1 / 39

And now for something completely different . . .

Learn about sequential and binary search algorithms, their advantages, and how to implement them in Python. Includes animations and videos.

lpetersen
Download Presentation

And now for something completely different . . .

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. AD c 2018 And now for something completely different . . . 1

  2. AD c 2018 Python Searching 2

  3. AD c 2018 Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search 3

  4. AD c 2018 Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search 4

  5. AD c 2018 Introduction to Searching The topics of searching and sorting occupy prominent positions in computer science. 5

  6. AD c 2018 It has been estimated that anywhere from 25% to 50% of all computing time in the commercial world is spent on searching and sorting data. 6

  7. AD c 2018 Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search 7

  8. AD c 2018 Sequential Search One of the most straightforward and elementary searches is the sequential search, also known as the linear search. 8

  9. AD c 2018 Sequential Search You start at the beginning of a sequence and go through each item one by one, in the order they exist in the list, until you find the item you are looking for. . . 9

  10. AD c 2018 A dictionary is a book that lists (in alphabetical order) the words of a language and against each word a description of its meaning. . . 10

  11. AD c 2018 blissn. perfect happiness 11

  12. AD c 2018 You're guaranteed to find any word in a dictionary, if you start at the beginning and check every word one after the other. . . 12

  13. AD c 2018 Linear search requires you to look at, on average, half of the elements in the list. 13

  14. AD c 2018 In the worst case scenario, you may have to search the entire list to find the item of interest. 14

  15. AD c 2018 Advantages of Sequential Search? Easy to implement Data does not need to be sorted 15

  16. AD c 2018 Sequential (Linear) Search Animation Click here to see the animation: http://cs.armstrong.edu/liang/animation/web/LinearSearchNew.html 16

  17. AD c 2018 A Sequential Search program list1 = [11,27,36,44,51,22,65,1,78] numbertofind = int(input("Enter a number\n")) found = 0 for i in list1: if numbertofind == i: print (numbertofind, " at index: ",list1.index(numbertofind)) found = 1 if found == 0: print ("Number not found") 10-01.py 17

  18. AD c 2018 Another Sequential Search program mylist = [10,11,3,4,55,12,23,14,16] n = len(mylist) print (n) for i in range(n): print (mylist[i], end=" ") search = int(input("\nPlease enter a number to search for: ")) print (search) found = False for i in range(n): if mylist[i] == search: found = True index = i print() if found == True: print (str(search) + " found at index " + str(index)) else: print (str(search) + " not found") 10-02.py 18

  19. AD c 2018 Sequential (Linear) Search Video Click here to see the video: https://youtu.be/eQN-EB8Wsls 19

  20. AD c 2018 Searching 10.1 Introduction to Searching 10.2 The Sequential Search 10.3 The Binary Search 20

  21. AD c 2018 Binary Search Binary search is a more specialized algorithm than sequential search as it takes advantage of the fact that the data has been sorted. . . 21

  22. AD c 2018 Binary Search The underlying idea of binary search is to divide the sorted data into two halves and to examine the data at the point of the split. 22

  23. AD c 2018 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 23

  24. AD c 2018 Binary Search Since the data is sorted, we can easily ignore one half or the other depending on where the value we're looking for lies in comparison to the value at the split. This makes for a much more efficient search than linear search. 24

  25. AD c 2018 Binary Search Algorithms A binary search algorithm involves breaking down the problem into a smaller version of itself, and hence is an ideal candidate for a technique known as recursion. . . 25

  26. AD c 2018 Recursion Recursion is a method for solving a problem in which the problem is broken down into a smaller version of itself. 26

  27. AD c 2018 Recursion A recursive solution to a programming problem uses a function which calls itself. We study recursion in detail in a separate unit. 27

  28. AD c 2018 Binary Search Binary search involves binary decisions, decisions with two choices. At each step in the process you can eliminate half of the data you're searching. . . 28

  29. AD c 2018 Binary Search If you have an list of 16 numbers (N = 16), you can find any one number in at most, 4 steps: 16 -> 8 -> 4 -> 2 -> 1 Log216 = 4 29

  30. AD c 2018 Searching for number 7 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 33? 30

  31. AD c 2018 Searching for number 7 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 10? 31

  32. AD c 2018 Searching for number 7 2 7 9 10 11 15 21 33 35 41 53 75 82 88 91 94 is 7 <= 7? 7 found! 32

  33. AD c 2018 33

  34. AD c 2018 Binary Search In general, binary search operates on one of two data structures: lists and trees.In this course, we focus on lists. 34

  35. AD c 2018 Binary Search Animation Click here to see the animation: http://cs.armstrong.edu/liang/animation/web/BinarySearchNew.html 35

  36. AD c 2018 Binary Search Video Click here to see the video: https://youtu.be/_G7ivNb6BPk 36

  37. AD c 2018 Binary Search Python Code Click here to see the binary search programs 10-04.p and 10-05.py: http://www.annedawson.net/python3programs.html 37

  38. AD c 2018 This presentation uses the following program files: 10-01.py to 10-05.py Click here to see Python code: http://www.annedawson.net/python3programs.html 38

  39. AD c 2018 Last updated: Friday 5th January 2018, 7:00 PT, AD 39

More Related