Assignment 3 DSA
Assignment 3 DSA
Index Sequential Search is a searching technique that combines elements of both sequential and
indexed searching. It uses an index table that holds pointers to records in a sorted dataset, allowing
for faster access. When searching for an element, the algorithm first checks the index to identify the
relevant block, then performs a sequential search within that block. This reduces the number of
comparisons needed, making it more efficient than basic sequential search, especially in large
datasets where direct access would be time-consuming.
Ans 2-
1. Initial Setup:
2. First Step:
Value at mid: 15
3. Second Step:
Value at mid: 4
4. Third Step:
Value at mid: 7
Value at mid: 9
The binary search process quickly narrows down the potential location of the target element by
repeatedly dividing the search interval in half, making it efficient for sorted arrays.
Ans 3-
Hash Function
Insertion Steps
1. Insert 50:
o Inserted at index 1.
2. Insert 700:
o Inserted at index 0.
3. Insert 76:
o Inserted at index 6.
4. Insert 85:
o Index: 85mod 7=185 \mod 7 = 185mod7=1
▪ i=1i = 1i=1
o Inserted at index 2.
5. Insert 92:
▪ i=1i = 1i=1
o Inserted at index 5.
6. Insert 73:
▪ i=1i = 1i=1
o Inserted at index 3.
Summary
The quadratic probing process efficiently resolves collisions by checking subsequent indices based on
the square of the number of attempts. This method helps maintain performance in a hash table by
reducing clustering compared to linear probing.
Ans 4-
Double Hashing
Double Hashing is an open addressing collision resolution technique used in hash tables. It employs
two hash functions to compute the index for storing and retrieving keys. When a collision occurs (i.e.,
when the calculated index is already occupied), double hashing uses the second hash function to
determine the next index to try.
How It Works
1. Hash Functions:
o Let h1(key)h_1(key)h1(key) be the primary hash function that computes the initial
index.
2. Insertion Process:
o If that index is occupied, compute the next index using the second hash function:
index=(index+h2(key))mod table sizeindex = (index + h_2(key)) \mod \text{table
size}index=(index+h2(key))modtable size
Minimizing Collisions
Double hashing minimizes collisions by using a secondary hash function that creates a varied step
size for each key. This approach ensures that keys that collide will probe different slots, reducing
clustering:
• Diverse Step Sizes: The second hash function can provide a range of possible probe
sequences, helping to distribute keys more uniformly across the hash table.
• Less Clustering: Unlike linear or quadratic probing, which can create groups of filled slots,
double hashing minimizes clustering by spreading out keys over the hash table.
Ans 5-
Quick Sort
Quick Sort is a divide-and-conquer sorting algorithm that works by selecting a "pivot" element from
the array and partitioning the other elements into two sub-arrays: those less than the pivot and
those greater than the pivot. The sub-arrays are then sorted recursively.
1. Choose a Pivot: Select an element from the array as the pivot (commonly the last element).
2. Partitioning:
o Rearrange the array so that elements less than the pivot come before it and those
greater come after it.
3. Recursively Apply:
o Recursively apply the same steps to the sub-arrays formed by splitting at the pivot.
Example
o Choose pivot: 5.
2. Partitioning:
3. Recursive Calls:
▪ Choose pivot: 7.
o Quick Sort has an average case time complexity of O(n*log n), making it efficient for
large datasets.
2. In-place Sorting:
o It sorts the elements in place, requiring less memory than algorithms that use
additional data structures.
3. Locality of Reference:
o Quick Sort performs well with modern computer architectures due to better locality
of reference, which keeps frequently accessed data close together in memory.
4. Good on Average:
o Although its worst-case time complexity is O(n2) (when the pivot is consistently the
smallest or largest element), this scenario is rare with good pivot selection strategies
(like choosing a random pivot).
Ans 6-
Merge Sort
Merge Sort is a divide-and-conquer algorithm that splits an array into halves, recursively sorts each
half, and then merges the sorted halves back together.
Given Array
1. Divide:
▪ Right: [5, 6, 7]
▪ Result: [5, 6]
o Now merge the two sorted halves [11, 12, 13] and [5, 6, 7]:
▪ Result: [5]
▪ Result: [5, 6]
▪ Result: [5, 6, 7]
▪ Add remaining 11, 12, 13: Result: [5, 6, 7, 11, 12, 13]
Summary
Merge Sort efficiently sorts the array by repeatedly dividing it into halves and merging sorted halves
back together. This algorithm has a time complexity of O(n*log n), making it effective for large
datasets.