Data Structures & AlgorithmsHardGATE CS · DSA
Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only?
Shell sort with gap sequence 4, 2, 1 (gapped insertion sort for each gap). What is the array after the gap-4 pass only?
a = [23, 17, 6, 15, 27, 25, 26, 1, 8]
gap = 4
for i in range(gap, len(a)):
x, j = a[i], i
while j >= gap and a[j - gap] > x:
a[j] = a[j - gap]; j -= gap
a[j] = x
print(a)- A. [8, 17, 6, 1, 23, 25, 26, 15, 27]
- B. [23, 17, 6, 1, 8, 25, 26, 15, 27]
- C. [8, 17, 6, 1, 25, 23, 26, 15, 27]
- D. [8, 17, 6, 1, 23, 25, 26, 27, 15]
Correct answer: A. [8, 17, 6, 1, 23, 25, 26, 15, 27]
Elements 4 apart form sub-lists that are each insertion-sorted; with 9 elements, index 0, 4, 8 form one chain, so an element can move two gaps. Output: [8, 17, 6, 1, 23, 25, 26, 15, 27].
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
- 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)
- Longest run of consecutive integers (in any order) using a hash set and starting only from numbers whose predecessor is absent. What is…
- Group words that are anagrams of each other using the sorted word as the key. What is printed? python from collections import defaultdict…
- Product of all elements except self, using prefix and suffix products (no division). What is printed? python a = [-1, 0, 2, 2, 2] n =…
- What is the maximum product of a non-empty contiguous subarray of [−2, 4, 3, −1, −2, −3, −2, 0]? (Track both the maximum and the minimum…
- Group words that are anagrams of each other using the sorted word as the key. What is printed? python from collections import defaultdict…