Sequential Search - Data Structures - Lecture Notes, Study notes of Data Structures and Algorithms

Some concept of Data Structures are Abstract, Balance Factor, Complete Binary Tree, Dynamically, Storage, Implementation, Sequential Search, Advanced Data Structures, Graph Coloring Two, Insertion Sort. Main points of this lecture are: Sequential Search, Linear Search, Faster Sequential, Search Code, Basic Operation, Value Causes, Loop the Fewest, Worst Case, Average, Number of Loops

Typology: Study notes

2012/2013

Uploaded on 04/30/2013

jut
jut 🇮🇳

4.5

(63)

77 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1. Consider the following sequential search (linear search ) code:
def linearSearch(aList, target):
"""Returns the index of target in aList
or -1 if target is not in aList"""
for position in range(len(aList)):
if target == aList[position]:
return position
return -1
def sequentialSearch(alist, item):
""" Sequential search of unorder list """
pos = 0
found = False
while pos < len(alist) and not found:
if alist[pos] == item:
found = True
else:
pos = pos+1
return found
Faster sequential search codeTextbook’s Listing 5.1
a) What is the basic operation of a search?
b) For the following
aList
value, which
target
value causes
linearSearch
to loop the fewest (“best case”)
number of times?
aList: 10 15 28 42 60 69 75 88 90 93 97
0 1 2 3 4 5 6 7 8 9 10
c) For the above
aList
value, which
target
value causes
linearSearch
to loop the most (“worst case”) number of
times?
d) For a successful search (i.e.,
target
value in
aList
), what is the “average” number of loops?
def linearSearchOfSortedList(target, aList):
"""Returns the index position of target in
sorted aList or -1 if target is not in aList"""
breakOut = False
for position in range(len(aList)):
if target <= aList[position]:
breakOut = True
break
if not breakOut:
return -1
elif target == aList[position]:
return position
else:
return -1
def orderedSequentialSearch(alist, item):
""" Sequential search of order list """
pos = 0
found = False
stop = False
while pos < len(alist) and not found and not stop:
if alist[pos] == item:
found = True
else:
if alist[pos] > item:
stop = True
else:
pos = pos+1
return found
Faster sequential search codeTextbook’s Listing 5.2
e) The above version of linear search assumes that
aList
is sorted in ascending order. When would this version
perform better than the original
linearSearch
at the top of the page?
Data Structures Lecture 14 Name:__________________
Lecture 14 Page 1
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Sequential Search - Data Structures - Lecture Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

  1. Consider the following sequential search (linear search ) code:

def linearSearch(aList, target): """Returns the index of target in aList or -1 if target is not in aList""" for position in range(len(aList)): if target == aList[position]: return position return -

def sequentialSearch(alist, item): """ Sequential search of unorder list """ pos = 0 found = False while pos < len(alist) and not found: if alist[pos] == item: found = True else: pos = pos+ return found

Textbook’s Listing 5.1 Faster sequential search code

a) What is the basic operation of a search?

b) For the following aList value, which target value causes linearSearch to loop the fewest (“best case”) number of times?

aList: 10 15 28 42 60 69 75 88 90 93 97

c) For the above aList value, which target value causes linearSearch to loop the most (“worst case”) number of times?

d) For a successful search (i.e., target value in aList), what is the “average” number of loops?

def linearSearchOfSortedList(target, aList): """Returns the index position of target in sorted aList or -1 if target is not in aList""" breakOut = False for position in range(len(aList)): if target <= aList[position]: breakOut = True break

if not breakOut: return - elif target == aList[position]: return position else: return -

def orderedSequentialSearch(alist, item): """ Sequential search of order list """ pos = 0 found = False stop = False while pos < len(alist) and not found and not stop: if alist[pos] == item: found = True else: if alist[pos] > item: stop = True else: pos = pos+ return found

Textbook’s Listing 5.2 Faster sequential search code

e) The above version of linear search assumes that aList is sorted in ascending order. When would this version perform better than the original linearSearch at the top of the page?

  1. Consider the following binary search code:

def binarySearch(target, lyst): """Returns the position of the target item if found, or -1 otherwise.""" left = 0 right = len(lyst) - 1 while left <= right: midpoint = (left + right) // 2 if target == lyst[midpoint]: return midpoint elif target < lyst[midpoint]: right = midpoint - 1 else: left = midpoint + 1 return -

def binarySearch(alist, item): first = 0 last = len(alist)- found = False while first<=last and not found: midpoint = (first + last)// if alist[midpoint] == item: found = True else: if item < alist[midpoint]: last = midpoint- else: first = midpoint+ return found

Textbook’s Listing 5.3 Faster binary search code

a) “Trace” binary search to determine the worst-case basic total number of comparisons?

0 1 2...^ n-1 target 151

midpoint

midpoint

elements

remaining

worst-case loop

1 "n"

left (^) right

b) What is the worst-case big-oh for binary search?

c) What is the best-case big-oh for binary search?

d) What is the average-case (expected) big-oh for binary search?

e) If the list size is 1,000,000, then what is the maximun number of comparisons of list items on a successful search?

f) If the list size is 1,000,000, then how many comparisons would you expect on an unsuccessful search?

  1. Consider the following examples using open-address approach with a simple rehashing strategy of linear probing where the hash table is scanned circularly from the home address until an empty hash table address is found.

Set of Keys

John Doe

John Doe hash(John Doe) = 6

Philip East

Philip East hash(Philip East) = 3

Mark Fienup

Mark Fienup hash(Mark Fienup) = 5

Ben Schafer

Ben Schafer

hash(Ben Schafer) = 8

hash(Paul Gray) = 3

hash(Kevin O'Kane) = 3

Hash function Hash Table Array

0 1 2 3 4 5 6 7 8 9

Paul Gray

Kevin O'Kane

a) Assuming open-address with linear probing where would Paul Gray and then Kevin O’Kane be placed?

Common rehashing strategies include the following.

Use the target key to determine an offset amount to be used each attempt, i.e., (home address + (rehash attempt #) * offset) % (hash table size), where the hash table size is a power of 2 and the offset hash returns an odd value between 1 and the hash table size

double hashing

Check the square of the attempt-number away for an available slot, i.e., (home address + ((rehash attempt #)^2 +(rehash attempt #))/2) % (hash table size), where the hash table size is a power of 2

quadratic probing

Check next spot (counting circularly) for the first available slot, i.e., (home address + (rehash attempt #)) % (hash table size)

linear probing

Description Rehash Strategy

b) Assume quadratic probing, insert “Paul Gray” and “Kevin O’Kane” into the hash table.

Lecture 14 Page 4

Set of Keys

John Doe

John Doe hash(John Doe) = 6

Philip East

Philip East hash(Philip East) = 3

Mark Fienup

Mark Fienup hash(Mark Fienup) = 5

Ben Schafer

Ben Schafer

hash(Ben Schafer) = 0

hash(Paul Gray) = 3

hash(Kevin O'Kane) = 3

Hash function Hash Table Array 0 1 2 3 4 5 6 7 3-

Paul Gray

Kevin O'Kane

Docsity.com

c) Assume double hashing, insert “Paul Gray” and “Kevin O’Kane” into the hash table.

Set of Keys

John Doe

John Doe hash(John Doe) = 6

Philip East

Philip East hash(Philip East) = 3

Mark Fienup

Mark Fienup hash(Mark Fienup) = 5

Ben Schafer

Ben Schafer

hash(Ben Schafer) = 0

hash(Paul Gray) = 3 rehash_offset(Paul Gray) = 1 hash(Kevin O'Kane) = 3 rehash_offset(Kevin O'Kane) = 3

Hash function Hash Table Array 0 1 2 3 4 5 6 7 3-

Paul Gray

Kevin O'Kane

d) For the above double-hashing example, what would be the sequence of hashing and rehashing addresses tried for Kevin O’Kane if the table was full? For the above example, (home address + (rehash attempt #) * offset) % (hash table size) would be: (4 + (rehash attempt #) * 3) % 8

Address

Rehash Attempt # 0 1 2 3 4 5 6 7 8 9 10

e) Indicate whether each of the following rehashing strategies suffer from primary or secondary clustering.

 primary clustering - keys mapped to a home address follow the same rehash pattern

 secondary clustering - rehash patterns from initially different home addresses merge together

Use the target key to determine an offset amount to be used each attempt, i.e., (home address + (rehash attempt #) * offset) % (hash table size), where the hash table size is a power of 2 and the offset hash returns an odd value between 1 and the hash table size

double hashing

Check a square of the attempt-number away for an available slot, i.e., (home address + ((rehash attempt #)^2 +(rehash attempt #))/2) % (hash table size), where the hash table size is a power of 2

quadratic probing

Check next spot (counting circularly) for the first available slot, i.e., (home address + (rehash attempt #)) % (hash table size)

linear probing

secondary clustering

primary clustering

Suffers from: Description Rehash Strategy

  1. Let λ be the load factor (# item/hash table size). The average probes with linear probing for insertion or

unsuccessful search is: (^12 )^1 + (^) (1−^1 ) 2. The average for successful search is: (^12 )^1 + (^) (1−^1 ).

a) Why is an unsuccessful search worse than a successful search?