Ds Expt1
Ds Expt1
Theory The find() method searches for a specified item by repeatedly dividing in half the
range of array elements to be considered. The method looks like this:
int curIn;
while(true)
{
curIn = (lowerBound + upperBound ) / 2;
if(a[curIn]==searchKey)
return curIn; // found it
else if(lowerBound > upperBound)
return nElems; // can’t find it
else // divide range
{
if(a[curIn] < searchKey)
lowerBound = curIn + 1; // it’s in upper half
else
upperBound = curIn - 1; // it’s in lower half
} // end else divide range
} // end while
} // end find()
The method begins by setting the lowerBound and upperBound variables to the first
and last occupied cells in the array. Setting these variables specifies the range where
the item we’re looking for, searchKey, may be found. Then, within the while loop,
the current index, curIn, is set to the middle of this range.
If we’re lucky, curIn may already be pointing to the desired item, so we first check if
this is true. If it is, we’ve found the item, so we return with its index, curIn.
Each time through the loop we divide the range in half. Eventually, the range will
get so small that it can’t be divided any more. We check for this in the next statement:
If lowerBound is greater than upperBound, the range has ceased to exist. (When
lowerBound equals upperBound, the range is one and we need one more pass through
the loop.) We can’t continue the search without a valid range, but we haven’t found
the desired item, so we return nElems, the total number of items. This isn’t a valid
index because the last filled cell in the array is nElems-1. The class user interprets
this value to mean that the item wasn’t found.
If curIn is not pointing at the desired item, and the range is still big enough, we’re
ready to divide the range in half. We compare the value at the current index,
a[curIn], which is in the middle of the range, with the value to be found,
searchKey.
If searchKey is larger, we know we should look in the upper half of the range.
Accordingly, we move lowerBound up to curIn. Actually, we move it one cell beyond
curIn because we’ve already checked curIn itself at the beginning of the loop.
If searchKey is smaller than a[curIn], we know we should look in the lower half of
the range. So we move upperBound down to one cell below curIn. Figure shows
how the range is altered in these two situations.
Algorithm/
Flow Chart
C -Program
1. Write a c program on linear search.
#include<stdio.h>
int linear_search(int arr [ ], int size, int target)
{
for (int i=0; i<size; i++)
{
if (arr [i] == target)
{
return i; //return the index if the target is found
}
}
return -1; //return -1 if the target is not found
}
int main()
{
int arr[ ]={2,4,6,8,10};
int target;
printf("enter the target:");
scanf("%d",&target);
int size=sizeof (arr) / sizeof (arr[0]);
int result=linear_search(arr,size,target);
if (result!=-1)
{
printf("element found at index %d\n", result);
}
else
{
printf("element not found\n");
}
return 0;
}
2. Write a c program on binary search.
#include<stdio.h>
int binary_search(int arr[], int size,int target)
{
int left=0;
int right=size-1;
while (left<=right)
{
int mid=left+(right-left)/2;
if (arr[mid]==target)
{
return mid; //return the index if the target is found
}
if (arr[mid]<target)
{
left=mid+1; //search in the right half
}
else
{
right=mid-1; //search in the left half
}
}
return -1; //if the target is not found
}
int main()
{
int arr[]={2,4,6,8,10,12,14,16,18,20};
int target;
printf("enter the target:");
scanf("%d",&target);
int size=sizeof(arr)/sizeof(arr[0]);
int result=binary_search(arr,size,target);
if (result!=-1)
{
printf("element found at index %d\n",result);
}
else
{
printf("element not found\n");
}
return 0;
}
3. Comparison of linear and binary search with a graph.
The elements can be in random order. The elements are in sorted order.
Access is slow. Access is faster.
Works with arrays and linked lists. Works well with arrays but not with linked
lists.
Graph:
Conclusion:
Conclusion
The major advantage is that search times are much faster than in an unordered array.
The disadvantage is that insertion takes longer because all the data items with a higher
key value must be moved up to make room. Deletions are slow in both ordered and
unordered arrays because items must be moved down to fill the hole left by the deleted
item. Ordered arrays are therefore useful in situations in which searches are frequent,
but insertions and deletions are not. An ordered array might be appropriate for a
database of company employees
We’ve seen that a binary search provides a significant speed increase over a linear
search. In the number-guessing game, with a range from 1 to 100, a maximum of
seven guesses is needed to identify any number using a binary search; just as in an
array of 100 records, seven comparisons are needed to find a record with a specified
key value. How about other ranges? Table shows some representative ranges and the
number of comparisons needed for a binary search.