Dsa 6 Alt
Dsa 6 Alt
ALGORITHMS
INTRODUCTION
This lab exercise focuses on the implementation of two fundamental searching algorithms:
Linear Search and Binary Search. Searching algorithms are critical in computer science as
they enable efficient data retrieval from various data structures. Linear Search is a basic
sequential method that checks each element until the target is found. Binary Search, in
contrast, is significantly more efficient but requires the dataset to be sorted. It works by
repeatedly dividing the search range in half, minimizing the number of comparisons.
THEORY
Searching is the process of finding the position or presence of a specific element within a
dataset, such as an array, list, or tree. It is a fundamental operation in computer science and
plays a vital role in numerous applications, including information retrieval, decision-making
systems, and data analysis.
Two commonly used searching techniques are:
1. Linear Search
Linear Search is a simple algorithm that checks each element of the array in sequence.
It does not require the dataset to be sorted.
Best suited for small or unsorted datasets.
Time complexity: O(n)
Algorithm
i. Start from the first element of the array.
ii. Compare each element with the key.
iii. If a match is found, return the index.
iv. If the end of the array is reached without a match, return -1.
2. Binary Search
Binary Search is a highly efficient algorithm, but it requires a sorted dataset.
It repeatedly divides the array into halves, eliminating the half where the key cannot exist.
Time Complexity: O(log n)
Algorithm
i. Set two pointers: low (start) and high (end) of the array.
ii. Calculate the middle index: mid = (low + high) / 2.
iii. Compare the middle element with the key:
1
iv. If it matches, return the index.
v. If the key is smaller, search in the left half.
vi. If the key is larger, search in the right half.
vii. Repeat until low > high. If no match is found, return -1.
TASK PERFORMED
I. Write a program to implement linear searching algorithm.
II. Write a program to implement binary searching algorithm.
SOURCE CODE
I.
#include<stdio.h>
int i;
int linearSearch(int A[],int n,int key){
for(i=0;i<n;i++){
if(A[i] == key)
return i;
}
return -1;
}
int main(){
int A[100],n,k,p;
printf("Enter the number of items, n:");
scanf("%d",&n);
printf("Enter the elements of the array:\n");
for(i=0;i<n;i++){
printf("A[%d]: ",i);
scanf("%d",&A[i]);
}
printf("Enter the data to search, k :");
scanf("%d",&k);
p=linearSearch(A,n,k);
if(p==-1){
printf("Data not found.");
} else {
printf("Data found at index %d",p);
}
return 0;
}
II.
2
#include<stdio.h>
int binarySearch(int A[],int l,int r,int key){
int m;
while(l<=r){
m=(l+r)/2; //integer division
if(key==A[m])
return m;
else if(key<A[m])
r=m-1;
else
l=m+1;
}
if(l>r)
return -1;
}
int main(){
int A[100],n,i,k,l,r,p;
printf("Enter the number of items, n:");
scanf("%d",&n);
printf("Enter the elements of the array in a sorted order:\n");
for(i=0;i<n;i++){
printf("A[%d]: ",i);
scanf("%d",&A[i]);
}
printf("Enter the data to search, k :");
scanf("%d",&k);
l=0; r=n-1; //set initial search boundaries
p=binarySearch(A,l,r,k);
if(p==-1){
printf("Data not found.");
} else {
printf("Data found at index %d",p);
}
return 0;
}
3
OUTPUT
I. Linear Search
Data input
4
DISCUSSION
During this lab session, we explored and implemented both Linear and Binary Search
algorithms. These implementations provided a hands-on understanding of how different
search strategies function depending on the structure and size of the dataset.
Linear Search is ideal for small and unsorted datasets due to its simplicity. However, its O(n)
time complexity makes it inefficient for large datasets, as it may require scanning every
element.
Binary Search, with a much more efficient O(log n) time complexity, significantly reduces
search time but requires the data to be sorted beforehand. This prerequisite can be a limitation
in some real-world scenarios, where sorting itself may add to the computational cost.
While testing, Linear Search demonstrated its usefulness in smaller or unsorted arrays, while
Binary Search showed superior speed in sorted datasets. This practical comparison helped in
understanding not only the algorithms but also how context and data structure affect their
efficiency.
Overall, this lab helped reinforce the importance of choosing the right search strategy based
on the nature of the data and performance requirements.
CONCLUSION
In conclusion, we successfully implemented and compared the Linear and Binary Search
algorithms. Linear Search, while easy to implement, is better suited for small or unsorted
datasets. Binary Search, though more efficient, depends on the dataset being sorted. Both
algorithms are fundamental in understanding how data can be searched efficiently. Mastering
these techniques lays the groundwork for exploring more advanced searching and
optimization strategies in data structures and algorithms.