+ All Categories
Home > Documents > Cs212: DataStructures

Cs212: DataStructures

Date post: 22-Feb-2016
Category:
Upload: rafiki
View: 45 times
Download: 0 times
Share this document with a friend
Description:
Cs212: DataStructures. Lecture 3: Searching. Lecture Contents. searching S equential search algorithm . B inary search algorithm . Search Algorithms. Searching , the process used to find the location of a target among a list of objects. - PowerPoint PPT Presentation
22
CS212: DATASTRUCTURES Lecture 3: Searching 1
Transcript
Page 1: Cs212:  DataStructures

CS212: DATASTRUCTURESLecture 3: Searching

1

Page 2: Cs212:  DataStructures

2

Lecture Contents searching

Sequential search algorithm. Binary search algorithm.

Page 3: Cs212:  DataStructures

3

Search Algorithms Searching , the process used to find the

location of a target among a list of objects.

In this chapter, we will study searches that work with arrays Search Algorithms

Sequential Search Binary Search

Page 4: Cs212:  DataStructures

4

Search Algorithms

1. Sequential search.1. It’s not requires an ordered list.

2. Binary search. It requires an ordered list.

Page 5: Cs212:  DataStructures

5

1/ Sequential (Linear) Search

Search an array or list by checking items one at a time.

Sequential search is usually very simple to

implement, and is practical when the list has only a few elements, or when performing a single search in an unordered list.

Look at every element : This is a very straightforward loop comparing every element in the array with the target(key). Eighter we find it, or we reach the end of the list!

Page 6: Cs212:  DataStructures

6

Locating data in unordered list.

Page 7: Cs212:  DataStructures

7

Sequential Search Algorithm The searching algorithm requires three

parameters:1. The list.2. An index to the last element in the list.3. The target.

Page 8: Cs212:  DataStructures

8

Sequential Search Algorithmalgorithm SeqSearch (val list <array>, val last

<index>, val target <keyType>)

Locate the target in an unordered list of size elements.

PRE list must contain at least one element. last is index to last element in the list. target contains the data to be located.

POST if found – matching index stored in Location. if not found – (-1) stored in Location

RETURN Location<integer>

Page 9: Cs212:  DataStructures

9

Sequential Search Algorithmlooker = 0loop (looker < last AND target not equal list(looker))

looker = looker + 1if (target == list[looker] )

Location= looker

elseLocation = -1

End Ifreturn Locationend SeqSearch

Page 10: Cs212:  DataStructures

10

Recursive Sequential Search Algorithm

algorithm RecSeqsearch (val I <integer>), val J <integer>) , val target<keyType>)Locate the target in an unordered list of size

elements.PRE I is is index to first element in the list.

last is index to last element in the list. target contains the data to be located.

POST if found – matching index stored in Location. if not found – (-1) stored in Location

RETURN Location<integer>

Page 11: Cs212:  DataStructures

11

Recursive Sequential Search Algorithm

if (A[I] == target) location = I

else if (I == J)location = -1else location = RecSeqsearch(I+1,J, target)

End IfReturn locationEnd RecSeqsearch

Page 12: Cs212:  DataStructures

12

Recursive sequential search (2)

Algorithm sequentialSearch (item<integer>,list<array>, listSize<integer>)Pre item contains a value, listSize contains the actual size of the array listPost find the location of item in listReturn either the item found and its location is returned or not and -1 returned     if (listSize == 0)       return -1    if (list[listSize-1] == item)       return listSize-1    else return sequentialSearch (item, list, listSize-1) End sequentialSearch

Page 13: Cs212:  DataStructures

13

2/ Binary search algorithm

Search a sorted array by repeatedly dividing the search interval in half.

A fast way to search a sorted array is to use a binary search.

Page 14: Cs212:  DataStructures

14

Binary search algorithm

Test the data in the element at the middle of the array.

it is in the first half before middle

it is in the second half after middle

Test the data in the element at the middle of the array.

Test the data in the element at the middle of the array.

it is in the second

half! it is in the second half!

it is in the first half!

it is in the first half!.. .. .. ..

Calculate the middle element

Calculate the middle element

If the middle element equals to the Target , the algorithm stops

Target < middle element Target > middle element

Target < middle Target < middle Target > middleTarget > middle

Calculate the middle element

Page 15: Cs212:  DataStructures

15

target > A[mid]first = mid +1

target == A[mid]

target < A[mid]last = mid -1

mid=(first+last)/2

Page 16: Cs212:  DataStructures

16

target < A[mid]last = mid -1

target > A[mid]first = mid +1

target < A[mid]last = first not found stop

target > A[mid]first = mid +1

Page 17: Cs212:  DataStructures

17

Recursive Binary search algorithmalgorithm RecBinarySearch (val First<index>, val

last <index>,val target <keyType>)Locate the target in an ordered list of size

elements.PRE list must contain at least one element.

First is index to first element in the list. last is index to last element in the list. target contains the data to be located.

POST if found – matching index stored in Location if not found – (-1) stored in Location

RETURN Location<integer>

Page 18: Cs212:  DataStructures

18

Recursive search algorithm

m := if target = am then

Location= melse if (first=last) then

Location= -1else if (target < am) then

Location =binarySearch(first, m-1, target)else if (target > am) then

Location=binarySearch(m+1, last, target ) Return LocationEnd RecBinarySearch

2/)( lastfirst

base cases

recursive calls

Page 19: Cs212:  DataStructures

19

Example[4] [3] [2] [1] [0]

20 11 7 5 3

BinarySearch (0,4,20)M=0+4/2=220 >7 then binarySearch(3 , 4,20)

BinarySearch (3,4,20)M=3+4/2=320 >11 then binarySearch(4 , 4,20)

BinarySearch (4,4,20)M=4+4/2=220 == 20

Return 4

Return 4

Return 4

Recursive call

Recursive call

Page 20: Cs212:  DataStructures

20

Binary search algorithm (iterative) Algorithm BinarySearch (list<array>, key <integer>, listSize<integer> ) Search an ordered list using Binary SearchPRE list must contain at least one element.

listSize is the actual size of the list. key contains the data to be located.

POST if found – matching index stored in Location if not found (-1) stored in Location

RETURN Location<integer>

Page 21: Cs212:  DataStructures

21

Binary search in iterative algorithm

first=0 last=listSize-1

while (first<= last) { mid = if ( list [mid] == key) return mid else if (list[mid] < key) first = mid + 1 else last = mid - 1}

return -1End BinarySearch

2/)( lastfirst

Page 22: Cs212:  DataStructures

22

References:• Text book, chapter2: Searching

End Of Chapter


Recommended