Dsa Lab Report

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

University of Engineering & Technology, Taxila

Name: Muhammad Ubaid Ashraf Chaudhary


Reg. no: 20-CP-09
Instructor: Muhammad Rizwan
Subject: Data Structure & Algorithms
Assignment: Code & Compare Algorithms 1
PART 1:
Linear Search Algorithm:
Defining function Linear_Search(int my_array[], int target)
Initializing values Int i = 0;
Running a loop for(i=0; i<=max; i++)
{
If(my_array[i]==target)
“Target data found at index ” i;
break;
}
if(i>max)
“Target data not found”
endfunction;
Binary Search Algorithm:
Define int Binary_Search(int a[],int first, int last, int to_find)
While(first<last)
{
Int middle = first + (last-first)/2;{
if(to_find==middle)
return middle;
else If(to_find<middle)
return Binary_Search(a,first,middle-1,to_find);
else
return Binary_Search(a,first,middle+1,to_find);
}
if(first>last)
return -1;
endfunction;
PART 2:
Linear Search:

Binary Search:
PART 3:
Comparison:
Linear Search Binary Search
Linear search algorithm searches each Binary search algorithm method is like
element in the list in the given order it checks the middle element and then
one by one till it finds the target data move to left or right ignoring the other
or reach the end of list half and the process continues till it
finds the target data or it fails to find
the target data in the list
There is no need of sorting the list as In binary search case, the list must be
Linear Search checks every element in sorted in ascending order so that it
the list one by one does not return the wrong answer
Linear search can take time if the list is Binary search is faster as compared to
too long because it checks sequentially linear search because it can eliminate
half of the list every time of execution
Examples:
Linear Search:
Assume that we have a list like: 8,4,6,5,10 and we want to find 9.
Linear search algorithm will check each number and match it with target to find if
it is in on the list or not. It will check 8,4,6,5 and then 10 in the same order and
display the results that if it found the target data or not.
Binary Search:
Assume we have another list which is a sorted one and that is: 2, 4, 6, 8, 10 and
we want to find 8.
Binary search will find the middle number by formula n+1/2 and in this case we
get 3rd element which is 6. The algorithm will ignore the left portion as the
required target 8 is larger than 6. Now again it will find the middle term by the
same formula which turn out to be 2nd element and it is 8. And that is how it will
find the target data so fast. In case if the target data is not present in the list, it
will go on checking until the left>right, it will display that target data is not found.
PART 4:
COMMENTS ON ALGORITHM PERFORMANCE:
Linear Search Algorithm:
In my view, linear search algorithm is an easier way of searching data as it does
not require us to put the data in sorted way. Though it is less efficient as
compared to Binary search because if the target data is not in the list or is at last
location then Linear Search will be very time consuming.
Binary Search Algorithm:
Binary Search Algorithm is more efficient as compared to linear search. It takes
much less time than Linear Search as it executes on the idea of checking the
middle term and then eliminating half of the list by comparing the required data
with the available data. The only limitation of Binary Search is that we can not
apply it to unsorted list. It requires the data in sorted way to find out the target
data.

You might also like