Searching Algorithms: Welcome To CS221: Programming & Data Structures
Searching Algorithms: Welcome To CS221: Programming & Data Structures
Searching Algorithms
• Topics covered are: Linear Search, Binary Search, Interpolation Search and Jump Search algorithms
• Note: The slides are adapted from the textbook “Data Structures Using C” by Reema Theraja, Oxford University Press.
• The textbook “Data Structures Using C” is available online
Linear Search/Sequential Search
Algorithm
Example:
Given a list of numbers a[] = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21}.
Search for value 19 using interpolation search technique.
Solution
Low = 0, High = 10, VAL = 19, a[Low] = 1, a[High] = 21
Middle = Low + (High – Low)×((VAL – a[Low]) /(a[High] – a[Low]))
= 0 +(10 – 0) × ((19 – 1) / (21 – 1) ) = 0 + 10 × 0.9 = 9
a[middle] = a[9] = 19 which is equal to value to be searched.
Complexity of Interpolation Search Algorithm
When n elements of a list to be sorted are uniformly
distributed (average case), interpolation search makes about
log(log n) comparisons. However, in the worst case, that is
when the elements increase exponentially, the algorithm can
make up to O(n) comparisons.
Jump Search/Block Search
• In jump search, it is not necessary to scan all the elements in Example:
the list to find the desired value.
Consider an array a[] = {1,2,3,4,5,6,7,8,9}. The length of the array
• We just check an element and if it is less than the desired is 9. If we have to find value 8 then following steps are performed
value, then some of the elements following it are skipped by using the jump search technique.
jumping ahead. After moving a little forward again, the
element is checked.
• If the checked element is greater than the desired value, then
we have a boundary and we are sure that the desired value lies
between the previously checked element and the currently
checked element.
• However, if the checked element is less than the value being
searched for, then we again make a small jump and repeat the
process.
• Once the boundary of the value is determined, a linear search
is done to find the value and its position in the array
Jump Search
Algorithm