To make binary search work well, there are a few important things we need to keep in mind:
Sorted Array: First, the data we want to search through should be sorted. This means it can be in increasing or decreasing order. Binary search looks at a sorted list to find the middle item for comparison.
Random Access: The data structure we use should allow for random access. This means that we can reach any item in the list quickly, in the same amount of time, no matter which item it is.
Non-duplicate Elements: While binary search can still work with duplicate items, having too many copies can slow it down. This is because it may need to check the same item multiple times.
Iterative or Recursive Implementation: We can set up the binary search in two ways: iteratively (using loops) or recursively (calling itself). Both methods take about the same time, which is pretty efficient, at .
By keeping these points in mind, we can use binary search effectively!
To make binary search work well, there are a few important things we need to keep in mind:
Sorted Array: First, the data we want to search through should be sorted. This means it can be in increasing or decreasing order. Binary search looks at a sorted list to find the middle item for comparison.
Random Access: The data structure we use should allow for random access. This means that we can reach any item in the list quickly, in the same amount of time, no matter which item it is.
Non-duplicate Elements: While binary search can still work with duplicate items, having too many copies can slow it down. This is because it may need to check the same item multiple times.
Iterative or Recursive Implementation: We can set up the binary search in two ways: iteratively (using loops) or recursively (calling itself). Both methods take about the same time, which is pretty efficient, at .
By keeping these points in mind, we can use binary search effectively!