You are on page 1of 4

LAB # 03

QUESTION # 01:
Assume array[]= {2,4,6,8,}. Give implementation of linear search algorithm to;
i. find element 8 in array
ii. find element 3 in array

Source Code (C++):


# include <iostream>
using namespace std;

int linearSearch(int DATA[], int N, int ITEM)


{
DATA[N] = ITEM;
int LOC = 0;
while (DATA[LOC] != ITEM)
LOC = LOC + 1;
if (LOC == N)
LOC = -1;
return LOC;
}

int main()
{
int DATA[4] = { 2, 4, 6, 8, };
cout << "Location of 8 is " << linearSearch(DATA, 4, 8) << endl;
cout << "Location of 3 is " << linearSearch(DATA, 4, 3) << endl;
return 0;
}

Output:

QUESTION # 02:
let assume A[]= {2,5,5,5,6,6,8,9,9,9} sorted array of integers containing duplicates, apply binary
search algorithm to Count occurrences of a number provided, if the number is not found in the
array report that as well. (Give implementation)

1
Source Code (C++):
# include <iostream>
using namespace std;

int binarySearch(int DATA[], int LB, int UB, int ITEM)


{
int BEG = LB, END = UB;
int MID = (BEG + END) / 2;
int LOC;
while (BEG <= END && DATA[MID] != ITEM)
{
if (ITEM < DATA[MID])
END = MID - 1;
else
BEG = MID + 1;
MID = (BEG + END) / 2;
}
if (DATA[MID] == ITEM)
LOC = MID;
else
LOC = -1;
return LOC;
}

int countOccurances(int DATA[], int LB, int UB, int ITEM)


{
int count = 0;
int LOC = binarySearch(DATA, LB, UB, ITEM);
if (LOC != -1)
{
count = 1;
int LEFT = LOC - 1, RIGHT = LOC + 1;

while (DATA[LEFT--] == ITEM)


count++;

while (DATA[RIGHT++] == ITEM)


count++;

return count;
}
}
int main()
{
int DATA[]= { 2, 5, 5, 5, 6, 6, 8, 9, 9, 9, };
int LB = 0, UB = 9;
int ITEM = 5;
int count = countOccurances(DATA, LB, UB, ITEM);
cout << "Count of " << ITEM << " is " << count << endl;
return 0;
}

2
Output:

QUESTION # 03:
Compare linear & binary Search algorithms on the basis of time complexity , which one is better
in your opinion? Discuss.

“COMPARITIVE ANALYSIS OF LINEAR SEARCH AND BINARY


SEARCH ON THE BASIS OF TIME COMPLEXITY”
The given chart compares the linear search and binary search algorithms on the basis of their
time complexities:

Linear Search Binary Search

O(n) O(log n)
The linear search algorithm finds an The binary search algorithm finds an
General Time item by comparing it with the items in item by keeping on dividing the array
Complexity an array in a linear fashion (from left into half repeatedly (or recursively)
to right) until an item matches. and comparing the middle element
with the item to be searched until it
matches.

O(1) O(1)
Best Case If the first item is the item to be If the left middle item is the item to be
Time searched, then only one comparison is searched, then only one comparison is
Complexity made and the algorithm takes constant made and the algorithm takes constant
time. time.

O(n) O(log n)
Worst Case
If the last item is the item to be If either the leftmost or the rightmost
Time
searched, then “n” comparisons are item is the item to be searched then
Complexity
made “⌊logn⌋” comparisons are made.

Which one is better?


3
Pros of Linear Search:
1. It works well in case of both sorted and unsorted arrays.
2. It is time efficient in case of small arrays.
3. If the element to be searched is very likely to be at the beginning, then it is the
recommended algorithm to be used.
4. Search operation can be done on multidimensional arrays as well.

Pros of Binary Search:


1. It is more time efficient.
2. It is a recommended choice if the data set is large. So for a million elements, linear search
would take an average of 500,000 comparisons, whereas binary search would take 20.
3. Better to use when the item to be searched is likely to be at the middle or the end of the
array

Conclusion:
Since under different conditions, the algorithms have their own benefits therefore, it cannot be
decided that which one of them is better to use. Hence, we consider both of the algorithms as the
best of their kind.

You might also like