Ds Unit 3
Ds Unit 3
UNIT – 3
First, we have to traverse the array elements using a for loop. In schools, roll number to retrieve information about
In each itera on, compare the search element with the that student.
current array element, and - A library has an infinite number of books. The librarian
If the element matches, then return the index of the assigns a unique number to each book. This unique
corresponding array element. number helps in identifying the position of the books on
If the element does not match, then move to the next the bookshelf.
element.
If there is no match or the search element is not present in Hash function and their types
the given array, return -1.
The hash function is used to arbitrary size of data to fixed-
Binary Search sized data.
It is search technique that works efficiently on sorted lists. hash = hashfunction(key)
we must ensure that the list is sorted.
Binary search follows the divide and conquer approach a. Division method :
Array divided into two parts and compared with middle
The hash func on H is defined by :
index of the element of the array
If the middle elements matched with the desired element H(k) = k (mod m) or H(k) = k (mod m) + 1
then we return the index of the element Here k (mod m) denotes the remainder when k is divided
Time complexity of the algo is O(logn) by m.
Example: k=53 , m=10 then h(53)=53mod10 =3
b. Midsquare method :
hash collision or hash clash is when two pieces of data in a Garbage collec on in hashing reclaims memory/resources
hash table share the same hash value from deleted elements that are no longer in use
It enhances hash table efficiency. Typically automa c, it's
Collision resolution technique managed by the data structure or language run me.
Mechanisms vary by language/implementa on.
We have two method to resolve this collision in our hashing .
these are following below : Insertion sort
1. Open addressing 2.seperate chaining
This is an in-place comparison-based sor ng algorithm.
1.Open addressing Here, a sub-list is maintained which is always sorted.
The array is searched sequen ally and unsorted items are
Open addressing stores all elements in the hash table itself. moved and inserted into the sorted sub-list (in the same
It systema cally checks table slots when searching for an array).
element. average and worst case complexity are of Ο(n2)
In open addressing, the load factor (λ) cannot exceed 1.
Load Factor (λ) = Number of Elements Stored / Total Number
of Slots
Probing is the process of examining hash table loca ons.
Linear Probing
it systema cally checks the next slot in a linear manner
un l an empty slot is found.
This process con nues un l the desired element is
located
method of linear probing uses the hash func on
h(k,i)= (k %m + i) mod m , where m is size of table
Quadra c Probing
it checks slots in a quadra c sequence (e.g., slot + 1, slot
+ 4, slot + 9, and so on) un l an empty slot is found.
This con nues un l the desired element is located or the
table is en rely probed.
method of Quadra c probing uses the hash func on Algorithm of Insertion sort
h(k,i)= (k %m + i^2) mod m
Double Probing 1. for j =2 to length[A]
it uses a second hash func on to calculate step size for 2. set key = A[j] and i=j-1
probing, providing a different sequence of slots to check. 3. while i > 0 and A[i] > key then
This con nues un l an empty slot is found or the desired 4. A[i + 1] = A[i]
element is located. 5. i=i–1
method of Quadra c probing uses the hash func on 6. endwhile
H1(k) = k%N and H2(k) = P - (k%P) 7. A[i + 1] = key
H(k, i) = (H1(k) + i*H2(k))%N 8. endfor
Where p is the prime Number less than k Bubble sort
Merge sort
Algorithm of Selection sort Merge sort is a sor ng algorithm that uses the idea of divide
and conquer.
1. Selection-Sort (A,n) : This algorithm divides the array into two subarray , sorts
2. for j = 1 to n – 1: them separately and then merges them.
3. sm = j
4. for i = j + 1 to n:
5. if A [i] < A[sm] then sm = i
6. Swap (A[j], A[sm])
Quick sort
radixSort(arr)
1. max = largest element in arr
2. d = number of digits in max
3. Now, create d buckets of size 0 - 9
4. for i -> 0 to d
5. sort the arr elements using counting sort
complexity of Radix sort :