39
Fri 5 th Jan 2018 AD c 2018 1 And now for something completely different . . .

And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 11

And now for something completely different . . .

Page 2: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 22

Python Searching

Page 3: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 33

Searching

10.1 Introduction to Searching

10.2 The Sequential Search

10.3 The Binary Search

Page 4: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 44

Searching

10.1 Introduction to Searching

10.2 The Sequential Search

10.3 The Binary Search

Page 5: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 55

Introduction to Searching

The topics of searching and sorting occupy prominent positions in computer science.

Page 6: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 6 6

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.

Page 7: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 77

Searching

10.1 Introduction to Searching

10.2 The Sequential Search

10.3 The Binary Search

Page 8: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 88

Sequential Search

One of the most straightforward and elementary searches is the sequential search, also known as the linear search.

Page 9: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 99

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. . .

Page 10: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1010

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

Page 11: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1111

bliss n. perfect happiness

Page 12: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1212

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

Page 13: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1313

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

Page 14: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1414

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

Page 15: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1515

Advantages of Sequential Search?

Easy to implement

Data does not need to be sorted

Page 16: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1616

Sequential (Linear) Search Animation

Click here to see the animation:http://cs.armstrong.edu/liang/animation/web/LinearSearchNew.html

Page 17: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1717

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

Page 18: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1818

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

Page 19: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 1919

Sequential (Linear) Search Video

Click here to see the video:

https://youtu.be/eQN-EB8Wsls

Page 20: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2020

Searching

10.1 Introduction to Searching

10.2 The Sequential Search

10.3 The Binary Search

Page 21: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2121

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. . .

Page 22: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2222

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.

Page 23: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2323

11 15 21 33 35 41 53 75 82 88 91 942 7 9 10

Page 24: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2424

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.

Page 25: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2525

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. . .

Page 26: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2626

Recursion

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

Page 27: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2727

Recursion

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

Page 28: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2828

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. . .

Page 29: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 2929

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

Page 30: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3030

11 15 21 33 35 41 53 75 82 88 91 942 7 9 10

Searching for number 7

is 7 <= 33?

Page 31: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3131

11 15 21 33 35 41 53 75 82 88 91 942 7 9 10

Searching for number 7

is 7 <= 10?

Page 32: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3232

11 15 21 33 35 41 53 75 82 88 91 942 7 9 10

Searching for number 7

is 7 <= 7? 7 found!

Page 33: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3333

Page 34: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3434

Binary Search

In general, binary search operates on one of two data structures: lists and trees.In this course, we focus on lists.

Page 35: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3535

Binary Search Animation

Click here to see the animation:http://cs.armstrong.edu/liang/animation/web/BinarySearchNew.html

Page 36: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3636

Binary Search Video

Click here to see the video:

https://youtu.be/_G7ivNb6BPk

Page 37: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3737

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

Page 38: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3838

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

Page 39: And now for something completely different · And now for something completely different . . . Fri 5th Jan 2018 AD c 2018 2 Python Searching. Fri 5th Jan 2018 AD c 2018 3 Searching

Fri 5th Jan 2018 AD c 2018 3939

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