Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector,...

15
Chapter 9 slide 1 Introduction to Search Algorithms • Search: locate an item in a list (array, vector, table, etc.) of information • Two algorithms (methods): – Linear search – Binary search

Transcript of Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector,...

Page 1: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 1

Introduction to Search Algorithms

• Search: locate an item in a list (array, vector, table, etc.) of information

• Two algorithms (methods):– Linear search– Binary search

Page 2: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 2

Sequential Search Algorithm on Unordered Array

set found to false set position to –1 set index to 0

while index < number of elts and found is false if list [index] is equal to search value

(key) found = true

position = index end If Add 1 to index

end Whilereturn position

Page 3: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 3

Linear Search Example

• Array numlist contains

• Searching for the the value 11, linear search examines 17, 23, 5, and 11

• Searching for the the value 7, linear search examines 17, 23, 5, 11, 2, 29, and 3

17 23 5 11 2 29 3

Page 4: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 4

Sequential Search Tradeoffs

• Benefits– Easy algorithm to understand– Array can be in any order

• Disadvantage– Inefficient (slow): for array of N elements,

examines N/2 elements on average for value in array, N elements for value not in array

Page 5: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 5

Improved Sequential Search Algorithm on Ordered Arrayset found to false

set position to –1 set index to 0

while index < number of items and list [index] <= key and found is false

if list [index] is equal to search value (key)

found = true position = index end If increment index

end whilereturn position

Page 6: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 6

Sequential Search on Ordered Array: Tradeoffs

• Benefits– Still easy algorithm to understand– More efficient : for array of N elements,

examines N/2 elements on average for value in array, N/2 elements for value not in array

• Disadvantage– Array MUST be in order

Page 7: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 7

Binary Search Algorithm on Ordered Array

1. Divide a sorted array into three sections.– middle element– elements on one side of the middle element– elements on the other side of the middle element

2. If the middle element is the correct value, done. Otherwise, go to step 1, using only the half of the array that may contain the correct value.

3. Continue steps 1 and 2 until either the value is found or there are no more elements to examine.

Page 8: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 8

Binary Search Example

• Array numlist2 contains

• Searching for the the value 11, binary search examines 11 and stops

• Searching for the the value 7, binary search examines 11, 3, 5, and stops

2 3 5 11 17 23 29

Page 9: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 9

Trace of Binary Search

info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

item = 45

first midPoint last

info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first midPoint last

LESS last = midPoint - 1

GREATER first = midPoint + 1

Page 10: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 10

Trace continued

info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

item = 45

first, midPoint, last

info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

first, last midPoint

LESS last = midPoint - 1

GREATER first = midPoint + 1

Page 11: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 11

Trace concludes

info[0] [1] [2] [3] [4] [5] [6] [7] [8] [9]

15 26 38 57 62 78 84 91 108 119

item = 45

last first

first > last found = false

Page 12: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 12

bool BinarySearch ( ArrayType info, ItemType& item, int length) // Purpose: To determine whether item is in the array info// Returns: If found, item’s key matches an element’s key in the list and a copy // of that element has been stored in item and returns true; otherwise, item is// unchanged and returns false{ bool fount = false; int midPoint ;

int first = 0; int last = length - 1 ;

bool moreToSearch = ( first <= last ) ;

while ( moreToSearch && !found ){ midPoint = ( first + last ) / 2 ; // INDEX OF MIDDLE ELEMENT

switch ( item.ComparedTo( info [ midPoint ] ) ) {

case LESS : last = middle -1; break; // LOOK IN FIRST HALF NEXT case GREATER : first = middle+1; break; // LOOK IN SECOND HALF

NEXT case EQUAL : found = true; // ITEM HAS BEEN FOUND

item.Copy ( info[midPoint] ); } moreToSearch = ( first <= last ) ;

} return found;}

Page 13: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 13

Binary Search Tradeoffs

• Benefit – Much more efficient than linear search(For array of N elements, performs at mostlog2N comparisons)

• Disadvantage – Requires that array elements be sorted

Page 14: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 14

Sequential Search Algorithm on Unordered Linked List

set found to false

set position to head

while position != null and found is false

if position->item.GetKey( ) == search value (key)

found = true

else

position = position-> next;

end while

return position

Page 15: Chapter 9 slide 1 Introduction to Search Algorithms Search: locate an item in a list (array, vector, table, etc.) of information Two algorithms (methods):

Chapter 9 slide 15

Sequential Search Algorithm on Ordered Linked List

set found to false

set position to head

while ( position != null and found is false and

position->item.GetKey( ) <= search value (key))

if position->item.GetKey( ) == search value (key)

found = true

else

position = position-> next;

end while

return position