0% found this document useful (0 votes)
45 views6 pages

Unit 5

Linear search sequentially compares a target item to each element in a list to check for a match. It has a worst case time complexity of O(n). Binary search works by dividing the search space in half at each step based on comparing the target to the middle element. It has a time complexity of O(log n). Hashing maps keys to memory addresses using a hash function, with the goal of uniform distribution to minimize collisions. Popular hash functions include division, mid-square, and folding methods.

Uploaded by

new born
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views6 pages

Unit 5

Linear search sequentially compares a target item to each element in a list to check for a match. It has a worst case time complexity of O(n). Binary search works by dividing the search space in half at each step based on comparing the target to the middle element. It has a time complexity of O(log n). Hashing maps keys to memory addresses using a hash function, with the goal of uniform distribution to minimize collisions. Popular hash functions include division, mid-square, and folding methods.

Uploaded by

new born
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Unit 5

Q1. Explain Linear search and give its algorithm

In this search particular element is search sequentially in the whole list. Suppose LA is a
linear array with N elements. Given no other information about LA, the most intuitive way to
search for a given ITEM in LA is to compare ITEM with each element of LA one by one.
To simplify the matter we first assign ITEM to LA[N+1], the position following the last
element of LA. Now LA[1] is compare with LA[n+1] if both same then LOC=LA[1] and if
not then LA[2] is compare with LA[n+1] and son in the end if LOC is found before the
LA[n+1] then search is successful otherwise search is not successful. The purpose of this
initial assignment is to avoid repeatedly testing whether or not we have reached the end of the
array DATA.
This method of searching is called linear search or sequential search.

Algorithm for linear search

(Here LA is a linear array with N elements, and ITEM is a given item of formation. This
algorithm finds the location of ITEM in LA.)

1. [Insert item at the end of LA]


set LA[N+1]:=ITEM
2. [Initialize counter]
set LOC:=1
3. [Search for item]
Repeat while LA[LOC] != ITEM
Set LOC:=LOC+1
[End of loop]
4. If LOC:=N+1 then search is successful else search is unsuccessful
5. Exit

Complexity of Linear search


Best base occurs if the item is found in the first comparision .ie O(1).

• Worst Case: Clearly the worst case occurs when ITEM is the last element in the array
DATA or is not there at all.

• Accordingly, T(n) =O(n) is the worst-case complexity of the linear search algorithm.

Average Case: Here we assume that ITEM does appear in DATA, and that is equally
likely to occur at any position in the array. Accordingly, the number of comparisons
can be any of the numbers 1,2,3,..., n, and each number occurs with probability p=1/n.
Then T(n) = O(n/2)

1
Q2. Explain Binary search , give its algorithm and time complexity?

Binary Search Method:


Suppose we are given a number of integers stored in an array A, and we want to locate a
specific target integer K in this array. If we do not have any information on how the integers
are organized in the array, we have to sequentially examine each element of the array. This is
known as linear search and would have a time complexity of O(n ) in the worst case.
However, if the elements of the array are ordered, let us say in ascending order, and we
wish to find out the position of an integer target K in the array, we need not make a
sequential search over the complete array. We can make a faster search using the
Binary search method.The basic idea is to start with an examination

The basic idea is to start with an examination of the middle element of the array. This
will lead to 3 possible situations:

If this matches the target K, then search can terminate successfully, by printing out the
index of the element in the array.

On the other hand, if K<A[middle], then search can be limited to elements to the left of
A[middle]. All elements to the right of middle can be ignored.

If it turns out that K >A[middle], then further search is limited to elements to the right of
A[middle].

If all elements are exhausted and the target is not found in the array, then the method
returns a special value such as –1.

Here is one algorithm the Binary Search function:


int BinarySearch (int A[ ], int n, int K) // A[] is array , n is size , R is variable
{
int L=0, Mid, R= n-1;
while (L<=R)
{
Mid = (L +R)/2;
if ( K= =A[Mid] )
return Mid;
else if ( K > A[Mid] )
L = Mid + 1;
else
R = Mid – 1 ;
}
return –1 ;
}

2
Complexity Analysis:
Let us now carry out an Analysis of this method to determine its time complexity. Since
there are no “for” loops, we can not use summations to express the total number of
operations. Let us examine the operations for a specific case, where the number of
elements in the array n is 64.

when n= 64 BinarySearch is called to reduce size to n=32


when n= 32 BinarySearch is called to reduce size to n=16
when n= 16 BinarySearch is called to reduce size to n=8
n= 8 BinarySearch is called to reduce size to n=4
n= 4 BinarySearch is called to reduce size to n=2
n= 2 BinarySearch is called to reduce size to n=1

Thus we see that BinarySearch function is called 6 times ( 6 elements of the array were
examined) for n =64.
Note that 64 = 2 pow 6
Also we see that the BinarySearch function is called 5 times ( 5 elements of the array
were examined) for n = 32.
Note that 32 = 2 pow 5
Let us consider a more general case where n is still a power of 2. Let us say n = 2 k .
Following the above argument for 64 elements, it is easily seen that after k searches, the
while loop is executed k times and n reduces to size 1.
Let us assume that each run of the while loop involves at most 5 operations.
Thus total number of operations: 5k.
The value of k can be determined from the expression
2 pow k = n
Taking log of both sides
k = log n
Thus total number of operations = 5 log n.
We conclude from there that the time complexity of the Binary search method is O(log
n), which is much more efficient than the Linear Search method.
Complexity of binary search is O(logn) (where n is the input size)

Q3. What is divide and conquere?

Divide-and-conquer algorithms revolve around 3 steps: divide, conquer and combine.


In the divide step, we divide the data into smaller, more manageable pieces. In the conquer
step, we process each division by performing some operations on it. In the combine step, we
recombine the processed divisions. An example of the divide-and-conquer algorithm is merge
sort.

Q4. Explain hashing and hash function?

The Search time of each algorithm depends on the number n of elements in the collection S
of data. Hashing or Hash addressing is a searching technique which is independent of the
number n. Assume that there is a file F of n records with a set K of keys which uniquely
determine the records in F.

Secondly we assume that F is maintained in memory by a table T of m memory locations and


that L is the set of memory addresses of the locations in T. we assume that the keys in K and
the addresses in L are (decimal) integers.

3
Example: Suppose a company has 68 employees, employees are assigned a 4-digit employee
number to each employee, which is used as the primary key. We can use the employee
number as the address of the record in memory.

How many searches are required for finding any key?

The search will require no comparison, but this technique will require space for 10,000
memory locations because the highest 4 digit number is 9999.

So the general idea of using the key to determine the address of a record is an excellent idea,
but it is further modified, so that a great deal of space is not wasted.

This modification takes the form of a function H from the set K of keys into the set L of
memory addresses.

Such a function

H:K->L

Is called a hash function or hashing function, unfortunately such a function H may not yield
distinct values, it is possible that two different keys k1 and k2 will yield the same hash
address.

This situation is called collision and some method must be used to resolve it.

Hashing is divided into two parts:

Hash Functions and Collision Resolutions.

Hash Functions

The principal criteria used in selecting a hash function H:K->L are as follows:

1. The function H should be very easy and quick to compute.

2. The function H should uniformly distribute the hash addresses throughout the set L so
that there are a minimum number of collisions.

There is no guarantee that the second condition will be fulfilled, however there are certain
techniques.

One technique is to “chop” a key K into pieces and combine the pieces in some way to form
the hash address H(K).

Some popular hash functions are as follows:

a) Division Method:

Choose a number m larger than the number n of keys in K. the number m is usually chosen to
be a prime number or a number without small divisors, since this frequently minimizes the
number of collisions.

4
b) Midsquare Method: The key k is squared, then the Hash Function H is defined as H(K)
= l where l is obtained by deleting digits from both ends of K2. The same positions of K2
must be used for all the keys.

c) Folding Method: The key K is partitioned into a number of parts k1,…., kr where each
part, except possibly the last, has the same number of digits as the required address, then the
parts are added together, ignoring the last carry. That is H(K) = K1 + K2 + …. + Kr where
the leading digit carries, if any, are ignored.

Sometimes the even-numbered parts K2, K4,… are each reversed before the addition.

Example:

Suppose a company with 68 employees, each employee is assigned a unique 4-digit employee
number, suppose L consists of 100 two-digit addresses i.e. 00, 01, 02,….99.

Apply the hash fuctions to each of the following employee numbers:

3205, 7148, 2345

Solution:

Division Method:

Choose a prime number m close to 99, consider m = 97, then

H(K) = K (mod m) = H(3205) = 3205 mod 97 = 4

H(7148) = 67, H(2345) = 17.

In the case the memory addresses begin with 01 rather than 00, we choose the function

H(K) = K(mod m) + 1

H(3205) = 3205 mod 97 + 1 = 4 + 1 = 5

H(7148) = 67 + 1 = 68, H(2345) = 17 + 1 = 18.

Midsquare Method:

Following calculations are performed:

K : 3205 7148 2345

K2 : 10272025 51093904 5499025

H(K) : 72 93 99

The fourth and the fifth digits, counting from the right are choosen for the hash address.

Folding Method:

Chopping the key K into two parts and adding yields the following hash addresses:
5
H(3205) = 32 + 05 = 37

H(7148) = 71 + 48 = 19

The leading 1 in H(7148) is ignored.

H(2345) = 23 + 45 = 68

Alternatively, folding method can be performed by reversing the second part before adding.

H(3205) = 32 + 50 = 82

H(7148) = 71 + 84 = 55

H(2345) = 23 + 54 = 77

You might also like