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 = [20, 16, 6, 28, 21, 3, 15, 38, 1]
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. [1, 3, 28, 6, 20, 16, 15, 38, 21]
- B. [1, 3, 6, 28, 20, 15, 16, 38, 21]
- C. [20, 3, 6, 28, 1, 16, 15, 38, 21]
- D. [1, 3, 6, 28, 20, 16, 15, 38, 21]
Correct answer: D. [1, 3, 6, 28, 20, 16, 15, 38, 21]
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: [1, 3, 6, 28, 20, 16, 15, 38, 21].
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
- A 2-D array A[0..9][0..19] of 4-byte elements is stored in row-major order starting at address 1000. What is the address of A[7][7]?
- For each day, a monotonic stack finds how many days you must wait for a strictly warmer temperature (0 if never). What is printed? python…
- Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed? python def delalt(h): p = h while…
- How many contiguous subarrays of [5, 3, 4, 5, 1, 1, 6, 5] have a sum between 6 and 10 inclusive? (All values positive: count atMost(10) −…
- Keys 80, 42, 55, 92, 96, 24, 95, 69 are inserted in this order into an empty hash table of size 11 with h(k) = k mod 11 and linear…
- Two pointers start at both ends of the sorted array [2, 5, 10, 11, 12, 14, 16, 18, 21, 29]. How many pairs (i < j) have a[i] + a[j] ≤ 28?…