Searching
Linear Search Linear Search Is it 1, 2, 3….
Linear Search Efficiency Linear Growth Best case: O(1) Worst case: O(n) Average: Check ~n/2 items = O(n)
Binary Search Binary Search Pick middle of remaining search space Too high? Eliminate middle and above Too low? Eliminate middle and below
Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4
Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4
Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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
Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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
Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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!!!
Binary Search Searching for 5: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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!!!
Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 3 4 (value at location 3) too small, need to search higher
Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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
Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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
Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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
Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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
Binary Search Searching for 6: Step minLocation maxLocation middleLocation middleValue 1 6 (1 + 6) / 2 = 3.5 = 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!
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
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
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
Binary Average Half the items will always be worst case Even if other half are free: 𝑎𝑣𝑒𝑟𝑎𝑔𝑒= 𝑤𝑜𝑟𝑠𝑡 2 = 𝑙𝑜𝑔 2 𝑛 2 =𝑂( 𝑙𝑜𝑔 2 𝑛)
Comparison Not even close…
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 -1 //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)
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 -1 //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)
Recursive Binary Search Recursion and BigO…
Recursive Binary Search Recursive cheat sheet: Binary search : Do constant work + time to solve half sized problem
Binary vs Linear Binary search requires sorted items How many searches do you need to do before it becomes worth sorting the items?
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
Break Even Break even curve for 𝑘𝑛=𝑛𝑙𝑜𝑔𝑛+𝑘𝑙𝑜𝑔𝑛