Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
4 views

Introduction

this is one of the best intoductions

Uploaded by

Suman Ghorai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Introduction

this is one of the best intoductions

Uploaded by

Suman Ghorai
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Introduction :- Binary search is an efficient algorithm

for finding an element from a sorted list of elements. It is a

searching algorithm based on the design paradigm of divide and

conquer. It gives us a pretty decent run time of O(log n) as

compared to the O(n) of linear search in worst-case scenarios. In

this algorithm, we recursively divide our array in half until we find

the element we are searching for or the list narrows down to one

element which does not match our element.

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.

What is Binary Search?


A binary search is an advanced type of search algorithm that finds and fetches
data from a sorted list of items. Its core working principle involves dividing the
data in the list to half until the required value is located and displayed to the
user in the search result. Binary search is commonly known as a half-interval
search or a logarithmic search.

How Binary Search Works?


For a binary search to work, it is mandatory for the target array to be sorted. We shall learn
the process of binary search with a pictorial example. The following is our sorted array and
let us assume that we need to search the location of value 31 using binary search.

First, we shall determine half of the array by using this formula −


mid = low + (high - low) / 2
Here it is, 0 + (9 - 0 ) / 2 = 4 (integer value of 4.5). So, 4 is the mid of the array.

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.

Hence, we calculate the mid again. This time it is 5.

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.

Procedure of Binary search :-


#include <stdio.h>
int binarySearch(int array[], int x, int low, int high) {
// Repeat until the pointers low and high meet each other
while (low <= high) {
int mid = low + (high - low) / 2;

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;
}

Why Do We Need Binary Search?


The following reasons make the binary search a better choice to be used as a
search algorithm:

 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

You might also like