Data Structures & AlgorithmsHardProduct placement · Amazon/Microsoft
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 = [35, 8, 28, 24, 32, 27, 13, 29, 19]
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. [32, 8, 13, 24, 19, 27, 28, 29, 35]
- B. [19, 8, 13, 32, 24, 27, 28, 29, 35]
- C. [19, 8, 13, 24, 32, 27, 28, 29, 35]
- D. [19, 8, 13, 24, 27, 32, 28, 29, 35]
Correct answer: C. [19, 8, 13, 24, 32, 27, 28, 29, 35]
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: [19, 8, 13, 24, 32, 27, 28, 29, 35].
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
- Keys 107, 29, 82, 81, 18, 115, 85 are inserted in this order into an empty table of size 17 using quadratic probing: probe i goes to (k…
- Keys 80, 25, 99, 55, 30, 13, 57, 42 are inserted in this order into an empty table of size 13 with h(k) = k mod 13 and linear probing…
- A queue is implemented with two stacks (push to IN; pop from OUT, refilling OUT from IN only when OUT is empty). Which statement is correct?
- ^ is exponentiation (highest precedence, right-associative); and / come next; + and − are lowest; others are left-associative. What is the…
- Selection sort (ascending; in pass i find the minimum of the unsorted part and swap it into position i only if it is not already there) is…
- Only the lower triangle (including the diagonal) of a 12 × 12 matrix is stored row by row in a 1-D array B starting at B[0] (indices…