Presentation is loading. Please wait.

Presentation is loading. Please wait.

Searching.

Similar presentations


Presentation on theme: "Searching."— Presentation transcript:

1 Searching

2 Linear Search Linear Search Is it 1, 2, 3….

3 Linear Search Efficiency
Linear Growth Best case: O(1) Worst case: O(n) Average: Check ~n/2 items = O(n)

4 Binary Search Binary Search Pick middle of remaining search space
Too high? Eliminate middle and above Too low? Eliminate middle and below

5 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4

6 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4

7 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower

8 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower

9 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 Found it!!!

10 Binary Search Searching for 5: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 5 7 (value at location 5) This is too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 Found it!!!

11 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 (value at location 3) too small, need to search higher

12 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower

13 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower

14 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher

15 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher

16 Binary Search Searching for 6: Step minLocation maxLocation
middleLocation middleValue 1 6 (1 + 6) / 2 = = 3 4 (value at location 3) too small, need to search higher 2 4 (one more than old middleLocation) 6 (unchanged) (4 + 6) / 2 = 10 / 2 = 5 7 (value at location 5) too big, need to search lower 3 4 (unchanged) 4 (one less than old middleLocation) (4 + 4) / 2 = 8 / 2 = 4 5 (value at location 3) too small, need to search higher 4 5 (one more than old middleLocation) 4 (unchanged) minLocation > maxLocation - we have nothing left to check - value is not there!

17 Binary Search Efficiency
Each step cuts search space in half Algorithm: Step # Possible Unchecked Numbers 100 1 50 2 25 3 12 4 6 5 7

18 Binary Search Efficiency
Logarithmic Growth Worst case = O(log2n) Num Items 10 50 100 500 1000 10000 100000 Binary Worst Case 4 6 7 9 14 17

19 Binary Average Half the items will always be worst case.
Assume list with indexes 1-16: Step Locations That Might Be Checked 1 8 2 4, 12 3 2, 6, 10, 14 4 1, 3, 5, 7, 9, 11, 13, 15

20 Binary Average Half the items will always be worst case
Even if other half are free: 𝑎𝑣𝑒𝑟𝑎𝑔𝑒= 𝑤𝑜𝑟𝑠𝑡 2 = 𝑙𝑜𝑔 2 𝑛 2 =𝑂( 𝑙𝑜𝑔 2 𝑛)

21 Comparison Not even close…

22 Recursive Binary Search
Binary search recursive: BinarySearch(key, array, size) return recurseBinarySearch(key, array, 0, size - 1) recurseBinarySearch(key, array, low, high) if(low > high) return //exhausted all possibilities mid = (low + high) / 2 if( array[mid] == key ) return mid else if( array[mid] > key ) return recurseBinarySearch(key, array, low, mid - 1) else return recurseBinarySearch(key, array, mid + 1, high)

23 Recursive Binary Search
What is BigO for recursive??? Constant work Function call to self Only one BinarySearch(key, array, size) return recurseBinarySearch(key, array, 0, size - 1) recurseBinarySearch(key, array, low, high) if(low > high) return //exhausted all possibilities mid = (low + high) / 2 if( array[mid] == key ) return mid else if( array[mid] > key ) return recurseBinarySearch(key, array, low, mid - 1) else return recurseBinarySearch(key, array, mid + 1, high)

24 Recursive Binary Search
Recursion and BigO…

25 Recursive Binary Search
Recursive cheat sheet: Binary search : Do constant work + time to solve half sized problem

26 Binary vs Linear Binary search requires sorted items
How many searches do you need to do before it becomes worth sorting the items?

27 Find in Unsorted w/Linear Time Sort and Find w/Binary Time
But… Can't sort a list in less than nlogn Linear search O(n), Binary search O(logn) Items Searched For Find in Unsorted w/Linear Time Sort and Find w/Binary Time 1 1*n nlogn + 1*logn 2 2*n nlogn + 2*logn k k*n nlogn + k*logn

28 Break Even Break even curve for 𝑘𝑛=𝑛𝑙𝑜𝑔𝑛+𝑘𝑙𝑜𝑔𝑛


Download ppt "Searching."

Similar presentations


Ads by Google