Data Structures & AlgorithmsVery hardTCS Digital · Infosys SP
Insertion sort (ascending; shift larger elements right, one key comparison per inner-loop test, the loop stops at the first element ≤ the key or at the start) sorts [10, 31, 49, 19, 47, 39, 50, 22]. How many element shifts and how many key comparisons does it make?
- A. 10 shifts, 18 comparisons
- B. 10 shifts, 17 comparisons
- C. 11 shifts, 17 comparisons
- D. 10 shifts, 10 comparisons
Correct answer: B. 10 shifts, 17 comparisons
Shifts equal the number of inversions (10). Each insertion makes one extra comparison that stops the loop unless the key travels to index 0, so comparisons = 17.
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- One Hoare partition with the FIRST element as pivot (i moves right while a[i] < pivot, j moves left while a[j] > pivot, swap while i < j…
- How many inversions (pairs i < j with a[i] > a[j]) does [5, 6, 10, 37, 38, 4, 17, 23] have? (This is also the number of swaps bubble sort…
- Keys 112, 95, 54, 144, 21, 31, 32 are inserted in this order into an empty table of size 11 by double hashing: h1(k) = k mod 11, h2(k) = 7…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- A checker counts how many strings are balanced over (), [] and {}. Version A uses a stack; version B only keeps one counter of open minus…
- A 2-D prefix-sum table answers rectangle queries in O(1). For the 4 × 4 matrix (rows separated by ';', 0-based) 1 3 7 6; 6 8 8 6; 1 9 7 8…