Self Studies

Algorithms Test 2

Result Self Studies

Algorithms Test 2
  • Score

    -

    out of -
  • Rank

    -

    out of -
TIME Taken - -
Self Studies

SHARING IS CARING

If our Website helped you a little, then kindly spread our voice using Social Networks. Spread our word to your readers, friends, teachers, students & all those close ones who deserve to know what you know now.

Self Studies Self Studies
Weekly Quiz Competition
  • Question 1
    1 / -0

    From the given options, which of the following sorting algorithm has the lowest-best case complexity?

    I. Merge Sort

    II. Quick sort

    III. Selection sort
    Solution

    Algorithm

    Best-case (Ω)

    Average-case (Θ)

    Worst-case(O)

    Merge sort

    n × log2n

    n × log2n

    n × log2n

    Quicksort

    n × log2 n

    n2

    n2

    Selection sort

    n2

    n2

    n2

     

    Worst best-case complexity is for Merge sort and Quick Sort

  • Question 2
    1 / -0
    In Hash table H, collision is resolved by using chaining. There are 29 slots in H. The average number of elements stored in a chain is 130. What is the total number of elements stored in H?
    Solution

    Data: 

    Total slots = total chain = 29

    Average no. of elements stored in a chain = load factor = 175

    Formula:

     \(load\; factor = \frac{Total\;elements}{number \;of \;slots}\)

    Calculation:

    \(130 = \frac{Total \;elements}{29}\)

    Total number of elements = 130 × 29 = 3770.

  • Question 3
    1 / -0

    The number of swappings needed to sort the numbers 8, 22, 7, 9, 31, 5, 13 in ascending order, using bubble sort is

    Solution

    Bubble sort repeatedly steps through the list to be sorted, compares each pair of adjacent items and swaps them if they are in the wrong order. The pass through the list is repeated until no swaps are needed, which indicates that the list is sorted.

    Array elements: 8, 22, 7, 9, 31, 5, 13

    1st pass = 8, 7, 9, 22, 5, 13, 31

    4 swaps

    2nd pass = 7, 8, 9, 5, 13, 22, 31

    3 swaps

    3rd pass = 7, 8, 5, 9, 13, 22, 31

    1 swap

    4th pass = 7, 5, 8, 9, 13, 22, 31

    1 swap

    5th pass = 5, 7, 8, 9, 13, 22, 31

    1 swap

    Since array is sorted after 5th pass

    ∴ no further swaps possible

    Total number of swaps = 4 + 3 + 1 + 1 + 1 = 10 

  • Question 4
    1 / -0

    Consider an empty Hash table of size 13 (0 -12). The keys inserted are 121, 142, 169, 30, 24, 78, 53, and 154. Hash Function is H(K) = K mod 13.Open addressing is the method used for collision resolution in a hash table with linear probing. What is the index of the key 154?

    Solution

    Keys: 121, 142, 169, 30, 24, 78, 53, 154

    Hash Function: K mod 13

    Hash Table:

    Index

    Values

    0

    169

    1

    78

    2

    53

    3

    154

    4

    121

    5

    30

    6

     

    7

     

    8

     

    9

     

    10

     

    11

    24

    12

    142

     

    154 mod 13 = 11

    By linear probing

    Index of 154 is 11.

  • Question 5
    1 / -0
    In a binary min -heap with ‘n’ elements, the 7-th smallest element can be found in the time of _________.
    Solution

    Delete once, get the smallest element in O(log n) time.

    Delete twice, get the second smallest element in 2 * log n time.

    So, for 7th smallest element the time required is 7 * log n  = O(log n).
  • Question 6
    1 / -0
    Let the length of sorted arrays of A1, A2, A3, A4, A5, and A6 be 41, 30, 15, 80, 25 and 50 respectively. To merge them into a single array A by merging two arrays at a time using optimal algorithm. In worst case, the maximum number of comparisons needed is _____.
    Solution

    The optimal algorithm always selects the smallest sequences for merging.

    Given sequence: 41, 30, 15, 80, 25, 50

    Ascending order: 15, 25, 30, 41, 50, 80

    Merge(15, 25) → new size (40)

    Number of comparisons = 25 + 15 – 1 = 39  

    Merge(30, 40) → new size (70)

    Number of comparisons = 30 + 40 – 1 = 69  

    Merge(41, 50) → new size (91)

    Number of comparisons = 41 + 50 – 1 = 90

    Merge(70, 80) → new size (150)

    Number of comparisons = 70 + 80 – 1 = 149  

    Merge(91, 150) → new size (241)

    Number of comparisons = 91 + 150 – 1 = 240

    Therefore, total number of comparisons, 39 + 69 + 90 + 149 + 240 = 587

  • Question 7
    1 / -0
    In an array A of 63 numbers consisting of a sequence of 0’s followed by a sequence of 1’s. The problem is to find the smallest index i such that A[i] is 1 by probing the minimum number of locations in A. The worst-case number of probes performed by an optimal algorithm is _____.
    Solution

    Worst case of the given problem is when a single 0 is followed by all 1’s i.e.

    0111111111111……

    Also, worst case can also be considered as when all 0’s are followed by single 1.

    000000000………………1

    Since in both the cases there is a sorted sequence of 0 and 1 and for sorted sequence binary search is preferred.

    At each stage, the element index is compared and if it is 1, search towards left and if it is 0 search towards the right.

    Total worst-case number of probes performed by an optimal algorithm = ⌈log2  63⌉ = 6

    Example:

    Optimal Algorithm: Binary search

    Consider element containing 7 elements

    Index

    0

    1

    2

    3

    4

    5

    6

    Element

    0

    0

    0

    0

    0

    0

    1

    1st probe

     

     

     

     

     

     

     

    Index

    0

    1

    2

    3

    4

    5

    6

    Element

    0

    0

    0

    0

    0

    0

    1

    2nd probe

     

     

     

     

     

     

     

    Index

    0

    1

    2

    3

    4

    5

    6

    Element

    0

    0

    0

    0

    0

    0

    1

    3rd probe

     

     

     

     

     

     

     

    Maximum probes = ⌈ log27 ⌉ = 3

    For 31 elements

    Maximum probes = ⌈ log2  63⌉ = 6

  • Question 8
    1 / -0

    An array of 50 distinct elements is to be sorted using quick sort. Assume that the pivot element is chosen uniformly at random. What is the probability that the pivot element gets placed in the worst possible location in the first round of partitioning?

    Solution

    Since the given array is sorted

    Let given array be 1, 2, 3, 4, 5, 6, 7, …. 50

    Probability of choosing each element is 1 ÷ 50

    Worst case for quick sort will only we arise if 1 is chosen or 50 is chosen as pivot element

    Assume 1 is chosen as a pivot element

    After first round of partitioning, pivot will be in its same position (1st position), this gives rise to worst case in quicksort since the complexity will be O(n2).

    Assume 25 is chosen as a pivot element

    After first round of partitioning, pivot will be in its same position (last position), this gives rise to worst case, in quicksort since the complexity will be O(n2).

    P(X = 1) = 1 ÷ 50 = 0.02

    and P(X = 50) = 1 ÷ 02 = 0.02

    P(X = 1 or X = 50)

    = 0.02 + 0.02

    = 0.04 

  • Question 9
    1 / -0

    In a double hashing scheme, the primary function is H1(K) = K mod 31, and the secondary hash function is H2(K) = 1 + (K mod 17). Hash table size is 31. If the probe sequence begins at probe 0 then the address returned by probe 1 in the probe sequence for key value K = 100 is _____. 

    Solution

    Data:

    N= 31, K= 100

    Formula:

    For double hashing method of collision resolution in hash tables, the location for key K is determined by:

    (H1(K) + i × H2(K)) mod N.

    where N is the size of the hash table and, i = 0, 1, 2, 3…….  is the probe sequence.

    Calculation:

    H1(K) = K mod 31 = 100 mod 31 = 7

    H2(K) = 1+( K mod 19) = 1 + 100 mod 17 = 1 + 15 = 16

    Address returned by probe corresponding to i = 1

    Retured address = (H1(K) + i ×H2(K)) mod N

    = (7 + 1 × 16) mod 31 = 23 mod 31 = 23

    The address returned by probe 1 is 23

Self Studies
User
Question Analysis
  • Correct -

  • Wrong -

  • Skipped -

My Perfomance
  • Score

    -

    out of -
  • Rank

    -

    out of -
Re-Attempt Weekly Quiz Competition
Selfstudy
Selfstudy
Self Studies Get latest Exam Updates
& Study Material Alerts!
No, Thanks
Self Studies
Click on Allow to receive notifications
Allow Notification
Self Studies
Self Studies Self Studies
To enable notifications follow this 2 steps:
  • First Click on Secure Icon Self Studies
  • Second click on the toggle icon
Allow Notification
Get latest Exam Updates & FREE Study Material Alerts!
Self Studies ×
Open Now