Data Structures & AlgorithmsHardTCS NQT · Infosys pattern
One Lomuto partition (pivot = last element, i marks the end of the 'smaller' part, swap when a[j] < pivot) is applied to [7, 2, 9, 4, 3, 8, 6]. What is the array afterwards?
- A. 2 4 3 6 9 8 7
- B. 4 2 3 6 7 9 8
- C. 2 4 3 6 7 8 9
- D. 2 3 4 6 9 8 7
Correct answer: A. 2 4 3 6 9 8 7
Elements smaller than 6 are swapped forward in the order met: 2 (swaps with 7), 4 (swaps with 9), 3 (swaps with 7), giving 2 4 3 7 9 8 6; finally the pivot swaps with a[3] = 7: 2 4 3 6 9 8 7. The right part is not sorted, and the left part keeps the order 4, 3.
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
- 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…
- 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…