Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
27 views5 pages

C++ Searching

Uploaded by

alswyah17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

C++ Searching

Uploaded by

alswyah17
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY

BANI WALID
Spring term : 2021-2022

Subject: CP II Lecturer Alhadi .A Khaleefah


Searching Techniques
Searching Techniques
We have two main searching techniques that are mostly employed to search for information.
These include:
 Linear Search
 Binary Search

Linear Search
This is the most basic searching technique and is easier to implement too. In a linear search, the
key to be searched is compared linearly with every element of the data collection. This
technique works effectively on linear data structures.

Above is the array of seven elements. If we want to search key = 10, then starting from the 0th
element, the key value will be compared to each element. Once the key element matches with
the element in the array, then that particular location will be returned. In this case location, 5
will be returned as the key-value matches the value at that location.
Example: Write C++ Language program to determine if the searching key avalble or not using
linear search?
#include <iostream>
#include <string>
using namespace std;
int main()
{
int myarray[10] = {21,43,23,54,75,13,5,8,25,10};
int key,loc;
cout<<"The input array is"<<endl;
for(int i=0;i<10;i++){
cout<<myarray[i]<<" ";
}
cout<<endl;
cout<<"Enter the key to be searched : "; cin>>key;
for (int i = 0; i< 10; i++)
{
if(myarray[i] == key)
{
loc = i+1;
break;
1
FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
BANI WALID
Spring term : 2021-2022

Subject: CP II Lecturer Alhadi .A Khaleefah


}
else
loc = 0;
}
if(loc != 0)
{
cout<<"Key found at position "<<loc<<" in the array";
}
else
{
cout<<"Could not find given key in the array";
}

Linear search can be performed on any linear data structure having sorted or unsorted elements.
But it takes a longer time if there are too many elements and if the key element is towards the
end as each element is compared with the key value.
Binary Search
Binary search is a technique that uses “divide and conquer” technique to search
for a key. It works on a sorted linear list of elements. The sorted list is the basic
requirement for a binary search to work.
In the binary search method, the list is repeatedly divided into half and the key
element is searched in both the halves of the list until the key is found.
For Example, let us take the following sorted array of 10 elements.

Let us say the key = 21 is to be searched in the array.


Let us calculate the middle location of the array.
Mid = 0+9/2 = 4

For Example, let us take the following sorted array of 10 elements.

2
FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
BANI WALID
Spring term : 2021-2022

Subject: CP II Lecturer Alhadi .A Khaleefah

Key = 21
First, we will compare the key value with the [mid] element. We find that the element value at
mid = 21.

Thus we find that key = [mid]. Hence the key is found.


key = 25

We first compare the key value to mid. So (21 < 25), we will directly search for the key in the
upper half of the array.

3
FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
BANI WALID
Spring term : 2021-2022

Subject: CP II Lecturer Alhadi .A Khaleefah

Now again we will find the mid for the upper half of the array.
Mid = 4+9/2 = 6
The value at location [mid] = 25

Now we compare the key element with the mid element. So (25 == 25), thus we have found the
key at location [mid].
We repeatedly divide the array and by comparing the key element with the mid, we decide in
which half to search for the key. Given below are the C++ for binary search.

#include <iostream>
#include <string>
using namespace std;
int binarySearch(int myarray[], int beg, int end, int key)
{
int mid;
if(end >= beg) {
mid = (beg + end)/2;
if(myarray[mid] == key)
{
return mid+1;
}
else if(myarray[mid] < key) {
return binarySearch(myarray,mid+1,end,key);
}

4
FACULTY OF ELECTRONIC ENGINEERING TECHNOLOGY
BANI WALID
Spring term : 2021-2022

Subject: CP II Lecturer Alhadi .A Khaleefah


else {
return binarySearch(myarray,beg,mid-1,key);
}
}
return -1;
}
int main ()
{
int myarray[10] = {5,8,10,13,21,23,25,43,54,75};
int key, location=-1;
cout<<"The input array is"<<endl;
for(int i=0;i<10;i++){
cout<<myarray[i]<<" ";
}
cout<<endl;
cout<<"Enter the key that is to be searched:"; cin>>key;
location = binarySearch(myarray, 0, 9, key);
if(location != -1) {
cout<<"Key found at location "<<location;
}
else {
cout<<"Requested key not found";
}
}

Binary search is more efficient in terms of time and correctness. Linear search technique is
seldom used as it is more cumbersome and slower. Binary search is a lot faster when compared
to a linear search.

Home work
Q1 Write C++ program using Linear search to find specific value in one
dimension array contains ten elements use functions ?

You might also like