7
Searching Algorithms Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index 0 to n and checked if it is the desired item Binary Search – a divide and conquer strategy where items are sorted from lowest to highest and the middle element is inspected first. If the target/desired item is lesser than the value in the middle, a binary search is again applied for the 1 st half of the elements (i.e. array). If the target is greater than the middle value, binary search is applied in the 2 half.

Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

Embed Size (px)

Citation preview

Page 1: Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

Searching AlgorithmsSearching Algorithms

Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index 0 to n and checked if it is the desired item

Binary Search – a divide and conquer strategy where items are sorted from lowest to highest and the middle element is inspected first. If the target/desired item is lesser than the value in the middle, a binary search is again applied for the 1st half of the elements (i.e. array). If the target is greater than the middle value, binary search is applied in the 2nd half.

Page 2: Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

Running TimeRunning Time

Sequential (serial) Search Sequential (serial) Search • simplest, O(n)simplest, O(n)

Binary SearchBinary Search• average-case O(log n)average-case O(log n)

Search by Hashing (during Hashing Search by Hashing (during Hashing Topic)Topic)• better average-case performancebetter average-case performance

Page 3: Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

SequentialSequential Pseudocode for Sequential SearchPseudocode for Sequential Search// search for a desired item in an array a of size n// search for a desired item in an array a of size n

set i to 0 and set found to false;set i to 0 and set found to false;

while (i<n && ! found)while (i<n && ! found){{ if (a[i] is the desired item) if (a[i] is the desired item) found = true;found = true; elseelse ++i;++i;}}

if (found) if (found) return i; // indicating the location of the desired return i; // indicating the location of the desired itemitemelseelse return –1; // indicating “not found”return –1; // indicating “not found”

Page 4: Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

Sequential SearchSequential Search

Size of array: nSize of array: n Best-Case: O(1)Best-Case: O(1)

• if item in [0]if item in [0] Worst-Case: O(n)Worst-Case: O(n)

• if item in [n-1] or not foundif item in [n-1] or not found Average-CaseAverage-Case

• usually requires fewer than n array usually requires fewer than n array accessesaccesses

44 33 88 66 55 11 22 77

4 6 7 9

Page 5: Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

What if there are 1 MILLION What if there are 1 MILLION elements in an array to be elements in an array to be

searched?searched?

What if there are 1 MILLION What if there are 1 MILLION elements in an array to be elements in an array to be

searched?searched? And what if the desired item is in And what if the desired item is in

the last cell of the array?the last cell of the array? Is sequential search works fast for Is sequential search works fast for

this situation? Yes OR No ?this situation? Yes OR No ?

Page 6: Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

Binary Search in an Integer Binary Search in an Integer ArrayArray

Binary Search in an Integer Binary Search in an Integer ArrayArray

Elements are sortedElements are sorted• target = 16target = 16• size = 8size = 8

Go to the middle location Go to the middle location mid = size/2mid = size/2

if (a[mid] is target)if (a[mid] is target)• done!done!

else if (target <a[mid]) else if (target <a[mid]) • go to the first half and go to the first half and

do binary search againdo binary search again else if (target >a[mid])else if (target >a[mid])

• go to the second half go to the second half and do binary search and do binary search againagain

22 33 66 77 1010 1212 1616 1818

[0] [1] [2] [3] [4] [5] [6] [7][0] [1] [2] [3] [4] [5] [6] [7]

Page 7: Searching Algorithms Sequential Search – inspects every items in a sequential manner. Example, in an array, all values in the array are checked from index

void bsearch (const int a[ ], size_t first, size_t size, int target, bool& found, size_t& location) { size_t middle;

if (size == 0) found = false; else { middle = first + size/2; if (target == a[middle]) { location = middle; found = true; } else if (target < a[middle]) // search the first half bsearch(a, first, size/2, target, found, location); else //search the second half bsearch(a, middle+1, size-1, target, found, location); } }

Binary Search Algorithm