Explain Linear Search and Binary search Algroithm with example

 Linear Search and Binary search Algroithm

Linear Search

Search an array for a key value
Linear search
Simple
Compare each element of array with key value
Useful for small and unsorted arrays

Binary search

For sorted arrays
Compares middle element with key
If equal, match found
If key < middle, looks in first half of array
If key > middle, looks in last half
Repeat
Very fast; at most n steps, where 2n > number of elements
30 element array takes at most 5 steps
25 > 30 so at most 5 steps

Labels: