Data Structures & AlgorithmsModerateCognizant · Capgemini pattern
Insertion sort (ascending) is applied to [5, 2, 4, 6, 1, 3]. What does the array look like after the first 3 passes (i.e. after inserting the elements at indices 1, 2 and 3)?
- A. 1 2 4 5 6 3
- B. 2 4 5 6 1 3
- C. 1 2 3 5 4 6
- D. 2 4 5 1 6 3
Correct answer: B. 2 4 5 6 1 3
Pass 1 inserts 2 → 2 5 4 6 1 3; pass 2 inserts 4 → 2 4 5 6 1 3; pass 3 inserts 6, which is already in place → 2 4 5 6 1 3. Only the prefix is sorted; 1 and 3 have not been touched yet. 1 2 4 5 6 3 is what you get after pass 4.
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
- Among insertion sort, merge sort, heap sort, quick sort, bubble sort and selection sort (standard array versions), how many are BOTH…
- LSD radix sort (base 10, stable counting sort per digit) is applied to [170, 45, 75, 90, 802, 24, 2, 66]. What is the array after the…
- What does this function return for a = [1, 3, 3, 3, 5, 7] with x = 3 and with x = 4? python def g(a, x): lo, hi = 0, len(a) - 1 while lo <…
- while (lo <= hi) binary search runs on a sorted array of 31 elements for a key LARGER than every element. How many times does the loop…
- A dynamic array starts with capacity 1 and doubles its capacity (copying all current elements) whenever an insert finds it full. How many…
- A stack-based checker is run on four strings. Which one is balanced?