Page 266 - DCAP201_FUNDAMENTALS_OF_DATA_STRUCTURES
P. 266
Unit 14: Searching
if(n<=0) Notes
return –1;
if(x[n]==ele)
return n;
else
return lin_search(x,n-1,ele);
}
Output
Enter Size:
5
Enter 5 elements;
12
30
25
4
85
The array elements:
12 30 25 4 85
Enter element to search:
25
The element 25 found at 3 location
Question
Write a program to search for a particular string from given set of strings using linear
search and binary search techniques.
Source: http://stylewap.comoj.com/wordpress/write-c-programs-that-use-both-recursive-and-non-
recursive-functions-to-perform-the-following-searching-operations-for-a-key-value-in-a-given-list-of-
integers-i-linear-search-ii-binary-search/
14.3 Summary
Linear search is the basic searching algorithm, also called as sequential search.
The complexity of linear search is O(N) because, in the worst case all items in the search
space will be examined.
The worst case performance scenario for a linear search is that it needs to loop through the
entire collection; either because the item is the last one, or because the item isn’t found.
Sequential search is the most common search used on linked list structures.
A binary search selects the median (middle) element in the array and compares its value
to that of the target value.
LOVELY PROFESSIONAL UNIVERSITY 259