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 [56, 48, 42, 34, 11, 45, 20, 8]. How many element shifts and how many key comparisons does it make?
- A. 24 shifts, 31 comparisons
- B. 25 shifts, 26 comparisons
- C. 24 shifts, 24 comparisons
- D. 24 shifts, 26 comparisons
Correct answer: D. 24 shifts, 26 comparisons
Shifts equal the number of inversions (24). Each insertion makes one extra comparison that stops the loop unless the key travels to index 0, so comparisons = 26.
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
- How many contiguous subarrays of [−2, 0, 5, −1, 4, 3, −1, −2, −3] have sum exactly 4? (Prefix sums with a hash map of prefix-sum…
- For s = "cdbccdddbaddca", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- For s = "bcadbdaccdddbd", what is the length of the longest substring with at most 2 distinct characters, and how many substrings (by…
- How many contiguous subarrays of [1, 3, 8, 1, 3, 6, 0, 0, 9] have a sum divisible by 3? (Count equal prefix-sum remainders.)
- Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only? python a = [23…
- What does this loop-counting code print? (Python) python n, c = 15, 0 for i in range(1, n + 1): j = 1 while j <= n: c += 1 j += i print(c)