Binary Search Algorithm in C++ with Source Code

Binary Search Algorithm in C++ Program

Binary Search Algorithm in C++ with Source Code

What is Binary search?

Binary search is one of the most important algorithms in the world. It allows us to quickly find a number in a sorted array of numbers or if there is a dictionary of words sorted lexicographically. We can quickly lookup a word. You must be very careful while implementing it. The study from 1988 showed that only one in four textbooks has a correct implementation of binary search. C++ projects for beginners with source code

In this lesson, we will talk about one of the most famous, and fundamental algorithms in computer science binary search.

• It saves time by reducing the number of comparisons needed to find the value.
• It reduces the search space to be searched by more than half which gives a significant improvement in the average case
• Binary search is a comparison sort, which runs in linear time in the size of the array being searched.

We find the application of binary search in a lot of problems in a wide variety of computer problems but here we are going to try to learn it. In its simplest form and to do so, we will first define a problem.

And the problem is given a sorted array of integers a sorted array means that the elements in the array are arranged either in increasing order or in decreasing order like in this array here the elements are arranged in increasing order. Let’s say the name of this array is A the size of this array is nine so we have an index starting zero-till eight.

Now given such an array and a number or an integer z we want to find out whether z exists in this array or not and if z exists in this array, then we want to find out the position at which z exists in this array so for example if z is 81 does 81 exist in the array yes 81 exists in the array A and it exists at index seven Does twenty-five exist in the array. C++ For Loop Programs Examples:

No. twenty-five does not exist in the array Does twenty-one exist in the array, yes twenty-one exists in that area at position three, at index three now what would be the logic to find out whether x exists in this array or not one simplest approach can be that we can scan the whole array to find out the desired number. We start at index zero and compare this element with x if it is equal to x, then we are done with our search, we are, the element in the array if not we go to the next element and we keep on comparing with the next element until either we are finished with the array or we find the number.

Let’s say if we wanted to find sixty-three in this array then our search will be over when we reach index six we start at index zero and our search will be over at index six if we wanted to find out twenty-five our search will be over at index eight with the conclusion that twenty-five does not exist in the array this approach will work irrespective of whether the array is sorted or not and if I have to write the code Let’s look now at the implementation of binary search. To implement binary search. and also Linear Search Algorithm Source Code in C++

Code:

#include<iostream>
#include<conio.h>

using namespace std;
void main()
{
int arr[100];
int key, ar_value, i, first, last;
cout<<“Enter Array Value. “;
cin>>ar_value;
cout<<“\nEnter Any “<<ar_value<<” Values.\n”;

   for (i = 0; i < ar_value; i++)
{
           cin>>arr[i];
}

       cout<<“\nEnter Find Value. “;

       cin>>key;

       cout<<“\nEnter First Value. “;

       cin>>first;

       cout<<“\nEnter Last Value. “;

       cin>>last;

       int mid=(first+last)/2;

       while(first <= last)

       {

              if (key == arr[mid])

              {

                     cout<<“\nValue found at “<<mid<<” index.”;

                     break;

              }

              else if (key > arr[mid])

              {

                     first = mid+1;

              }

              else

              {

                     last = mid -1;

              }

              mid=(first+last)/2;

       }

       if (first > last)

       {

              cout<<“\nValue not found!!!”<<endl;

       }

       getch();

}

#include<iostream>
#include<conio.h>

using namespace std;

void main()
{
	int arr[100];
	int key, ar_value, i, first, last;

	cout<<"Enter Array Value. ";
	cin>>ar_value;
	cout<<"\nEnter Any "<<ar_value<<" Values.\n";
	for (i = 0; i < ar_value; i++)
	{
		cin>>arr[i];
	}
	cout<<"\nEnter Find Value. ";
	cin>>key;
	cout<<"\nEnter First Value. ";
	cin>>first;
	cout<<"\nEnter Last Value. ";
	cin>>last;
	int mid=(first+last)/2;
	
	while(first <= last)
	{
		if (key == arr[mid])
		{
			cout<<"\nValue found at "<<mid<<" index.";
			break;
		}
		else if (key > arr[mid])
		{
			first = mid+1;
		}
		else
		{
			last = mid -1;
		}
		mid=(first+last)/2;
	}
	if (first > last)
	{
		cout<<"\nValue not found!!!"<<endl;
	}
	getch();
}

Output:

Binary Search Algorithm in C++ with Source Code

Top 40 projects with C++ for beginners in 2021

The C Programming Language Structure with C Language data types

Best Top 5 Programming Languages demand in 2021

C# Projects with Source Code

C# windows form application projects with source code

Linear Search Algorithm Source Code in C++

Leave a Comment

Your email address will not be published. Required fields are marked *