Data Structures & AlgorithmsHardGATE CS · DSA
One Lomuto partition (pivot = LAST element; i marks the end of the 'less than pivot' region; when a[j] < pivot, increment i and swap a[i], a[j]; finally swap the pivot into position i + 1) is applied to [30, 56, 7, 33, 29, 39, 57, 32]. What is the resulting array?
- A. [7, 29, 30, 32, 33, 39, 56, 57]
- B. [30, 7, 29, 32, 56, 39, 57, 33]
- C. [30, 7, 29, 32, 56, 33, 39, 57]
- D. [29, 7, 56, 33, 30, 39, 57, 32]
Correct answer: B. [30, 7, 29, 32, 56, 39, 57, 33]
Smaller elements are swapped forward in scan order (so their relative order is kept here, but the larger ones get shuffled). The pivot 32 lands at index 3, its final sorted position: [30, 7, 29, 32, 56, 39, 57, 33].
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
- 9 people stand in a circle numbered 1 to 9. Counting starts at person 1, and every 3rd person is removed (counting continues from the next…
- A hash-map two-sum returns the first index pair (i, j), i < j, found while scanning left to right. What is printed? python def twosum(a…
- A sorted array was rotated to give [29, 47, 50, 57, 60, 61, 75, 4, 5, 8, 13]. Binary search finds the index of the minimum: while lo < hi…
- Lines of heights [6, 9, 5, 6, 8, 1, 8, 3, 5] stand at x = 0, 1, …, 8. Two pointers find the pair of lines that, with the x-axis, holds the…
- Exponential search: double the bound i (1, 2, 4, …) until a[i] ≥ key or i passes the end, then binary-search the last range. How many…
- A Python 2-D list is created two ways and one cell is set. What is printed? python a = [[0] 3] 3 b = [[0] 3 for in range(3)] a[0][0] = 5…