Introduction
Introduction
the element we are searching for or the list narrows down to one
What is Search?
Search is a utility that enables its user to find documents, files, media, or any
other type of data held inside a database. Search works on the simple principle
of matching the criteria with the records and displaying it to the user. In this
way, the most basic search function works.
Now we compare the value stored at location 4, with the value being searched, i.e. 31. We
find that the value at location 4 is 27, which is not a match. As the value is greater than 27
and we have a sorted array, so we also know that the target value must be in the upper portion
of the array.
We change our low to mid + 1 and find the new mid value again.
low = mid + 1
mid = low + (high - low) / 2
Our new mid is 7 now. We compare the value stored at location 7 with our target value 31.
The value stored at location 7 is not a match, rather it is more than what we are looking for.
So, the value must be in the lower part from this location.
We compare the value stored at location 5 with our target value. We find that it is a match.
We conclude that the target value 31 is stored at location 5.
Binary search halves the searchable items and thus reduces the count of comparisons to be
made to very less numbers.
if (array[mid] == x)
return mid;
if (array[mid] < x)
low = mid + 1;
else
high = mid - 1;
}
return -1;
}
int main(void) {
int array[] = {3, 4, 5, 6, 7, 8, 9};
int n = sizeof(array) / sizeof(array[0]);
int x = 4;
int result = binarySearch(array, x, 0, n - 1);
if (result == -1)
printf("Not found");
else
printf("Element is found at index %d", result);
return 0;
}
Binary search works efficiently on sorted data no matter the size of the
data
Instead of performing the search by going through the data in a
sequence, the binary algorithm randomly accesses the data to find the
required element. This makes the search cycles shorter and more
accurate.
Binary search performs comparisons of the sorted data based on an
ordering principle than using equality comparisons, which are slower and
mostly inaccurate.
After every cycle of search, the algorithm divides the size of the array into
half hence in the next iteration it will work only in the remaining half of
the array
Conclusion :-
Search is a utility that enables its user to search for documents, files, and
other types of data. A binary search is an advanced type of search
algorithm that finds and fetches data from a sorted list of items.
Binary search is commonly known as a half-interval search or a
logarithmic search
It works by dividing the array into half on every iteration under the
required element is found.
The binary algorithm takes the middle of the array by dividing the sum of
the left and rightmost index values by 2. Now, the algorithm drops either
the lower or upper bound of elements from the middle of the array,
depending on the element to be found.
The algorithm randomly accesses the data to find the required element.
This makes the search cycles shorter and more accurate.
Reference:-
1) Data structure with C By Yashvent Kantekar
2) Data structure using C By Reema Tharaja
3) Data structure and algorithms made easy
4) Introduction to algorithms By Clifford stein