Lab 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

1.

Write a program to search an element in a 1-D array using Linear


Search and Binary Search.

Theory: It is the process of looking/searching for a specific element in the given


list of elements. Basically, the following are the two widely used searching
Techniques – Linear Search & Binary Search
1. Linear Search: It is the process of comparing the given searching element s
with each and every element in the given list of elements A.
A is a list of elements,
s is the element we want to search in A.
Output: Returns the position of the s in A, if the element is in A.
Returns -1, if the element is not in A.

• Compares the searching element s with each and every element of the
array/List (data structure), if the searching element s found in list A then it
returns the location of the searching element from the list of elements,
otherwise it returns the -1 – sequential search.
• Linear search is mostly used if the array contains an unordered list of
elements.
• For ex: int A [] = {10, 12, 23, 75, 3, 4, 9, 1, 8, 5, 14, 34, 56, 76, 31, 26};
searching element s = 8.
• s is found at position 9.

Algorithm – Linear Search


int Linear_Serach(Element Type A[], Element Type s, int n)
// n is the number of elements in A, s is the searching element and A is the array
// containing the elements

Step 1 – Let i := 0
Step 2: Compare the searching element s with the ith element in list A.
Step 3 - If both are matched, then return i+1.
Step 4 - If both are not matched, then increment i. If i is less than n and
then repeat the steps 2 and 3.
Step 5 - Return -1. // Element not found

Time Complexity = O(n)


// In the worst case it is making n comparisons

ine e x mple
ist o e ements
s

1. Binary Search
The input for the Binary search is the sorted elements (pre constraint).
Idea - Compare the searching element with the middle element in the list. There
are three possibilities
1. If the middle element is greater than the searching element then the
searching element may be found only in the first half of the elements
(Searching array size reduced to half).
2. If the middle element is less than the searching element then the
searching element may be found only in the second half of the
elements (Searching array size reduced to half)
3. If the middle element matches with the searching element, then
return the position of the middle element.
• The above three steps are repeated until it matches or elements exhausted.
Algorithm:

int inar _ earch ement_ pe A[] int ow int high ement_ pe s


// low is the lower index of the list, high is the higher index of A, and s is the searching element
1. et i ow j high
2. whi e i< j
i. m ow high /
ii. i A[m] s
i. ret rn m.
iii. e se i A[i] > s
i. j m – .
iv. e se
i. i m .
3. Ret rn // ement not o n

inar earch amp e


=

mi /

ota n m er o comparisons og
Time Complexity:

The recurrence equation of binary search is:

T(n) = T(n/2) + 1 // After one comparison, the size of the problem reduced to n/2

App master’s theorem


a = 1, b= 2, 𝑛𝑙𝑜𝑔𝑏𝑎 = 𝑛𝑙𝑜𝑔21
f(n) = 𝑛𝑙𝑜𝑔𝑏𝑎 (Solution is Θ(f(n) logn)
T(n) = Θ 1 * log2n) = Θ(log n)

2. Write a program to input n numbers in an array and print the sum of


only those whose tenth-place digit is divisible by 5.

Idea: Initialize the sum to zero. Access each element from the array and extract
its tenth-place digit and divide it with 5, if the remainder is zero then add the
element to the sum.

Algorithm
Element_Type Sum((Element_Type a[], int n) // n is the number of elements and a[] is
the array

1. sum  0, i 0
2. whi e i < n
a. i a[i]/ % %5
i. sum = sum + a[i]
b. Increment i one
3. Return sum

Time Complexity is O(n)

You might also like