|
|
|
|
|
|
Array |
|
Search algorithm |
|
Linear Search |
|
Binary Search |
|
|
|
|
|
|
|
|
|
|
|
A common requirement of many programs is to
search a list (array) for a given element. |
|
Linear Search |
|
Binary Search |
|
|
|
|
In a linear search, which is also known as a
sequential search, each item in the list is examined in the order in which
it occurs in the list until the desired item is found or the end of the
list is reached. |
|
Disadvantage and advantages: |
|
Š Not efficient |
|
a The algorithm is simple |
|
a The list need not be in any
particular order |
|
|
|
|
// function prototype |
|
int linearSearch(int list[], int size, int
key); |
|
|
|
// function definition |
|
// this function returns the location
of key in the list |
|
// -1 is returned if the value is not found |
|
int linearSearch(int list[], int size, int
key) { |
|
for (int i = 0; i < size; i++ ) { |
|
if (list[i] == key) return i; |
|
} |
|
return -1; |
|
} |
|
|
|
|
// function call |
|
int grade[5] = {98, 87, 74, 65, 82}; |
|
int key; |
|
cout << "Enter the item you are
searching for: "; |
|
cin >> key; |
|
|
|
cout << "The location is :"
<< linearSearch(grade,5,key) |
|
<< endl; |
|
|
|
|
|
|
|
In a binary search, the list must be in sorted
order. |
|
The desired item is first compared to the
element in the middle of the list (for lists with an even number of
elements, either of the two middle elements can be used). |
|
Three possibilities present once the comparison
is made: |
|
Case 1: the desired item may be equal to the
middle element, |
|
Case 2: it may be greater than the middle
element, |
|
Case 3: or it may be less than the middle
element. |
|
|
|
|
In the first case, the search has been
successful, and no further searches are required. |
|
In the second case, since the desired item is
greater than the middle element, if it s found at all it must be in the
upper part of the list. This means that the lower part of the list
consisting of all elements from the list to the midpoint elements can be
discards from any further search. |
|
In the third case, since the desired item is
less than the middle element, if it s found at all it must be in the lower
part of the list. This means that the upper part of the list consisting of
all elements from the midpoint to the end of the list can be discards from
any further search. |
|
|
|
|
|
|
// function prototype |
|
int binarySearch(int list[], int size, int key); |
|
|
|
// function definition |
|
// this function returns the location of key in the list |
|
// -1 is returned if the value is not found |
|
int binarySearch(int list[], int size, int key)
{ |
|
int left, right, middle; |
|
|
|
left = 0; |
|
right = size -1; |
|
(refer to next slide) |
|
|
|
|
|
|
while (left <= right) { |
|
middle = (left + right) / 2; // find the middle element |
|
// compare the desired element with the middle |
|
if
(key == list[middle]) { // if equal, return the position of the middle |
|
return middle; |
|
} |
|
else
if (key > list[middle]) { // if desired element is greater than the
middle |
|
left = middle + 1; // change the left pointer |
|
} |
|
else
{ // if desired element is less than the middle |
|
right = middle -1; // change the right pointer |
|
} |
|
} |
|
return -1; // if none of the element
matches, return -1 |
|
} |
|
|
|
|
|
|
// function call |
|
int list[] = {12, 23, 32, 45, 57, 68}; |
|
int key; |
|
cout << "Enter the item you are
searching for: "; |
|
cin >> key; |
|
|
|
cout << "The location is :"
<< binarySearch(list, 6, key)
<< endl; |
|
|
|