0% found this document useful (0 votes)
24 views4 pages

Pdsa Ga3

Uploaded by

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

Pdsa Ga3

Uploaded by

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

Graded Assignment 3

Q1) Consider the Quick sort algorithm in which the partitioning procedure splits elements into two sub-arrays and each
sub-array contains at least one-third of the elements. Let T(n) be the number of comparisons required to sort array of n
elements
A1)
T(n)<=T(n/3)+T(2n/3)+cn

Q2) Which function correctly inserts an element after x nodes in a linked list, where each node of linked list is an object of
class Node and x>=0 ? In the options below, head is the first node in the linked list and value is the value that needs to
be inserted after x nodes. The function insertAtBeginning(head, v) inserts a node at the beginning of the list with value v .

A2)

Q3) Which of the following code can be used as a queue?


A3)

Q4) Consider a Quicksort implementation where we first find the median then use the median as a pivot. Assume that we
have a O(n) time to find the median of an unsorted list. What will be the worst-case time complexity of this modified
Quicksort to sort n distinct elements?

A4)
O(nlogn)

Q5)

The above function can be used as partitioning function for sorting an array through Quicksort. What will be the state of
Array after the call 'partition([13, 18, 8, 10, 21, 7, 2, 32, 6, 19], 0, 9)' return?

A5)
[7,6, 8, 10, 2, 13, 21, 32, 18, 19]

Q6) Linear probing is an open addressing scheme in computer programming for resolving hash collisions in hash
tables. Linear probing operates by taking the original hash index and adding successive values linearly until a free slot is
found.

An example sequence of linear probing is:

h(k)+0, h(k)+1, h(k)+2, h(k)+3 .... h(k)+m-1

where m is a size of hash table, and h(k) is the hash function.


Hash function

Let h(k) = k mod m be a hash function that maps an element k to an integer in [0, m−1], where m is the size of the table.
Let the ith probe position for a value k be given by the function

h'(k,i) = (h(k) + i) mod m

The value of i = 0, 1, . . ., m – 1. So we start from i = 0, and increase this until we get a free block in hash table.

Consider inserting the keys 24, 36, 58, 65, 62, 79 into a hash table of size m = 11 using linear probing, the primary hash
function is h(k) = k mod m. What will be the hash table after inserting all keys in given order? Suppose initial hash table
is:
[None,None,None,None,None,None,None,None,None,None,None]

A6)
[None,None,24,36,58,79,None,62,None,None,65]

Q7) In a quicksort algorithm, we choose the last element in the array as pivot for partitioning. For which of the following
arrays will this algorithm exhibit the worst-case behavior?
A7)

[2, 3, 5, 7, 8, 13, 34, 46, 67]


[67, 46, 34, 13, 8, 7, 5, 3, 2]

Q8) In a linked list, what is the asymptotic worst-case running time for finding the size of the list?

A8) O(n)

Q9) For Quicksort on an input size n, where we always select the first element as pivot, how many partitioning levels
required in worst-case and best-case, respectively?

A9)
n, logn

Q10) Consider two lists L1 and L2 both containing n integers. We need to find if lists L1 and L2 are disjoint. Two lists are
disjoint, if there is no integer common in both the lists.
For example.

1. L1 = [5, 2, 7 , 1] and L2 = [3, 5], these two lists are not disjoint as 5 is common in both lists.

2. L1 = [2, 7, 1] and L2 = [3, 5], these two lists are disjoint as there is no integer common in both.

Select the most efficient solution or solutions for this in terms of asymptotic running time complexity from the below list.
A10)

Sort list L1, then for each integer i in L2, use binary search to find if i is present in L1 or not.
Sort both lists L1 and L2, then use merge like process to compare integers in both the lists.

You might also like