Explain Linear Search in Sorted Array Algorithm with Example in Data Structure.
Sorted Arrays
a[i] is 'less than or equal to' a[i+1] for i = left..right-1
Meaning of 'less than or equal to' can vary
need a method of testing whether or not 'less than or equal to' is true
Linear Search in Sorted Array
Search for target in a[left..right] This is an O(n) algorithm.
1. Loop using p = left..right
1.1 If a[p] greater than or equal to target then exit loop
2. If a[p] equals target then return index p else return 'not found'
Labels: Data Structure