Binary search’s are one of the most common and efficient ways to search through big lists of data
Linear Search and Binary Search
- The elements are in random order
- Linear Search looks through items until it finds the searched value.
- Worst case time complexity: 0(n)
- Scan one item at a time
- Single and multi dimensional array is sorted used
- The elements are sorted order
- Binary Search finds the middle element of the array. Checks that middle value is greater or lower than the search value. If it is smaller, it gets the left side of the array and finds the middle element of that part. If it is greater, gets the right part of the array. We continue this process until the element is found. Or if there is no value in the array finishes the search.
- worst case time complexity: 0(log n)
- Scan many items at a time
- only single dimensional array is sorted used
Which kind of data can we use binary search for ?
- Your data must be comparable (e.g, 1 < 5 or A < C)
- Your data must be sorted (e.g, 1,2,3,4,5 or A,C,F,H)
A nature, universe, science, music, love lover