Exp 2
Exp 2
Experiment No: 02
Aim: To implement Linear and Binary search and analyse its time complexity.
Theory:
Linear Search:
Linear search is a sequential searching algorithm where we start from one end and check
every element of the list until the desired element is found. It is the simplest searching
algorithm.
Algorithm:
● Step 1 − Start from the 0th index of the input array, compare the key value with the
value present in the 0th index.
● Step 2 − If the value matches with the key, return the position at which the value was
found.
● Step 3 − If the value does not match with the key, compare the next element in the
array.
● Step 4 − Repeat Step 3 until there is a match found. Return the position at which the
match was found.
● Step 5 − If it is an unsuccessful search, print that the element is not present in the
array and exit the program.
//Program:
#include<iostream>
using namespace std;
int count;
int linearSearch(int arr[],int size,int target){
for(int i=0;i<size;i++){
count++;
if(arr[i]==target){
count++;
return arr[i];
count++;
}
count++;
}
count++;
return -1;
}
int main(){
int size;
cout<<"Enter the size of array: ";
cin>>size;
int arr[size];
cout<<"Enter elements: ";
for(int i=0;i<size;i++){
count++;
cin>>arr[i];
}
count++;
int target;
cout<<"Enter the target: ";
cin>>target;
if(linearSearch(arr,size,target)){
count++;
cout<<"Element found"<<endl;
}
else{
count++;
cout<<"Element not found"<<endl;
}
cout<<"Time complexity: "<<count<<endl;
return 0;
}
//Output:
a. Worst Case:
b. Average Case:
c. Best Case:
Binary Search:
Binary Search is defined as a searching algorithm used in a sorted array by repeatedly
dividing the search interval in half. The idea of binary search is to use the information that
the array is sorted and reduce the time complexity to O(log N).
Algorithm:
● Divide the search space into two halves by finding the middle index “mid”.
● Compare the middle element of the search space with the key.
● If the key is found at the middle element, the process is terminated.
● If the key is not found at the middle element, choose which half will be used as the
next search space.
1. If the key is smaller than the middle element, then the left side is used for the
next search.
2. If the key is larger than the middle element, then the right side is used for the
next search.
● This process is continued until the key is found or the total search space is
exhausted.
//Program:
#include<iostream>
using namespace std;
int count;
bool binarySearch(int arr[],int size,int target){
int start = 0;
int end = size-1;
while(start<end){
count++;
int mid = start+(end-start)/2;
int element = arr[mid];
if(element==target){
count++;
return arr[mid];
}
else if(element>target){
count++;
end= mid-1;
}
else{
count++;
start = mid+1;
}
}
count++;
return -1;
}
int main(){
int size;
cout<<"Enter the size: ";
cin>>size;
int arr[size];
cout<<"Enter the elements: ";
for(int i=0;i<size;i++){
count++;
cin>>arr[i];
}
count++;
int target;
cout<<"Enter the target: ";
cin>>target;
return 0;
}
//Output:
a. Worst Case:
b. Average Case:
C. Best Case:
Conclusion: The implementation of the searching techniques and analysing their time
complexity was done successfully.