Download presentation
Presentation is loading. Please wait.
Published byBaldwin Gardner Modified over 9 years ago
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 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 nd half.
2
Running Time Sequential (serial) Search Sequential (serial) Search simplest, O(n)simplest, O(n) Binary Search Binary Search average-case O(log n)average-case O(log n) Search by Hashing (during Hashing Topic) Search by Hashing (during Hashing Topic) better average-case performancebetter average-case performance
3
Sequential Pseudocode for Sequential Search Pseudocode for Sequential Search // search for a desired item in an array a of size n set i to 0 and set found to false; while (i<n && ! found) { if (a[i] is the desired item) if (a[i] is the desired item) found = true; found = true; else else ++i; ++i;} if (found) return i; // indicating the location of the desired item return i; // indicating the location of the desired itemelse return –1; // indicating “not found” return –1; // indicating “not found”
4
Sequential Search Size of array: n Size 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 found if item in [n-1] or not found Average-Case Average-Case usually requires fewer than n array accessesusually requires fewer than n array accesses 43865127 4679
5
What if there are 1 MILLION elements in an array to be searched? And what if the desired item is in the last cell of the array? And what if the desired item is in the last cell of the array? Is sequential search works fast for this situation? Yes OR No ? Is sequential search works fast for this situation? Yes OR No ?
6
Binary Search in an Integer Array Elements are sorted Elements are sorted target = 16target = 16 size = 8size = 8 Go to the middle location mid = size/2 Go to the middle location mid = 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 do binary search againgo to the first half and do binary search again else if (target >a[mid]) else if (target >a[mid]) go to the second half and do binary search againgo to the second half and do binary search again 236710121618 [0] [1] [2] [3] [4] [5] [6] [7]
7
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.