Savitribai Phule Pune University
Second Year of Computer Engineering and Computer Science and Engineering (2024 Course)
PCC-201A COM: Data Structures Laboratory
Problem Statement:
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:
a) Linear Search: Check if a particular customer account ID exists in the list.
b) Binary Search: Implement Binary search to find if a customer account ID exists,
improving the search efficiency over the basic linear
Algorithm:
1. Linear Search:
1. Start
2. Accept Customer ID from user that you want to search in e-commerce system i:e target_id
3. You have to check one by one linearly that target_id is present inside the Customer_Id list.
3.1 if it is present then print or show target _id is present at nth index. or
3.2 if all customer_id scanned or checked and still if you are not getting match of target_id then just print or show customer_id is not present.
4. Stop
2. Binary Search:
1. Start
2. For Binary Search all elements inside the list must be in sorted order. hence sort the list (if not already sorted).
3. Set low = 0
(start index of
the list).
4. Set high = length of the list - 1
(end index).
5. While low
is less than or equal
to high
,
do:
5.1 Calculate mid
= (low + high) // 2
.
5.2 If the element
at mid
is equal to the target:
5.2.1 Return
mid
(target found).
5.3 Else if element
at mid
is less than the target:
5.3.1 Set
low = mid + 1
(search in right half).
5.4 Else:
5.4.1 Set
high = mid - 1
(search in left half).
6. If the loop ends and the target is
not found:
6.1 Return -1
.
Implementation:
No comments:
Post a Comment