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

Exp 2

The document discusses linear and binary search algorithms. It provides pseudocode to implement linear search in O(n) time and binary search in O(log n) time. For linear search, it checks each element sequentially until the target is found. Binary search works on a sorted array, repeatedly dividing the search space in half and checking the middle element to direct the next search space. The document includes C++ programs to implement both algorithms and track time complexity by counting operations. It concludes that both searching techniques were successfully implemented and their time complexities analyzed.

Uploaded by

roughuse24c
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)
21 views5 pages

Exp 2

The document discusses linear and binary search algorithms. It provides pseudocode to implement linear search in O(n) time and binary search in O(log n) time. For linear search, it checks each element sequentially until the target is found. Binary search works on a sorted array, repeatedly dividing the search space in half and checking the middle element to direct the next search space. The document includes C++ programs to implement both algorithms and track time complexity by counting operations. It concludes that both searching techniques were successfully implemented and their time complexities analyzed.

Uploaded by

roughuse24c
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

Date: 06/02/24

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;

int ans = binarySearch(arr,size,target);


if(ans!=1){
count++;
cout<<"Element not found";
}
else{
count++;
cout<<"Element found";
}
cout<<"Time complexity: "<<count<<endl;

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.

You might also like