Assignment no 1
Aim: In an e-commerce system, customer account IDs are stored in a list, and you
are tasked with writing a program that implements the following:
•Linear Search: Check if a particular customer account ID exists in the list.
•Binary Search: Implement Binary search to find if a customer account ID exists,
improving the search efficiency over the basic linear
Prerequisites: Knowledge of searching operation and arrays/lists.
Objectives: To understand the concept of Linear Search& Binary Search.
Input:
o Size of Array
o Elements in Array
Outcome:
Result of searching by using linear search & Binary search.
Description:
The linear search is a sequential search, which uses a loop to step through an array,
starting with the first element. It compares each element with the value being
searched for, and stops when either the value is found or the end of the array is
encountered. If the value being searched is not in the array, the algorithm will
unsuccessfully search to the end of the array. Since the array elements are stored in
linear order searching the element in the linear order make it easy and efficient.
The search may be successful or unsuccessfully. That is, if the required element is
found them the search is successful otherwise it is unsuccessfully.
Advantages Linear Search
Easy to understand.
It operates on both sorted and unsorted list.
It does not require array to be in order.
Easy to implement
Disadvantages Linear Search
Linear search is not efficient when list is large.
Maximum no. of comparison are N(n Element).
Not suitable for large problem.
Linear search is slower.
Linear Search Algorithm
Consider an integer type array A with size n. So list of elements from that
array are,
A [0], A [1], A [2], A [3]… A [n-1]
1. Declare and initialize one variable which contains the number to be search
in an array A.( variable key is declared)
2. Start Comparing each element from array A with the key LOOP:
A[size] == key
Repeat step no 2 while A[size] key
3. if key is found, display the location of element(index+1) or else display
message KEY NOT FOUND
4. Terminate the program successfully
Complexity of linear search:
1. Best Case = O(1)
2. 2.Average Case = O(n)
3. 3.Worst case = O(n)
Binary Search
- “Binary search is an searching algorithm which is used to find element from
the sorted list”. Algorithm can be applied only on sorted data Mid =
(lower+upper)/2 - formula used to find mid Given element is compared with
middle element of the list. If key=mid then element is found Otherwise list
divide into two part.(key <mid) (>mid) First to mid-1or mid+1 to last. If
given element is less than middle element then continue searching in first
part (first+mid-1) otherwise searching is second part(mid+1 to last).Repeat
above step still element is found.
Binary Search algorithm (Iterative)
• Binary Search(array, key):
low -> 0
high -> length(array) - 1 #As the indexing starts from zero
while low <= high:
mid -> (low + high) / 2
if array[mid] = key:
return mid
else if array[mid] < key:
low -> mid + 1
else:
high -> mid - 1
return not_ found
Advantages Binary Search
Binary search is optimal searching algorithms.
Excellent time efficiency.
Suitable for large list.
Faster because no need to check all element.
Most suitable for sorted array. It can be search quickly.
Time complexity O (log n).
Conclusion: Hence, we have performed linear and binary search successfully.
Questions:
1. What is linear search in data structure?
2. What is Binary search in data structure?
3. What is linear search time complexity?
4. What is Binary search time complexity?