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 [33, 32, 25, 47, 58, 19, 40, 36]. What is the resulting array?
- A. [19, 25, 32, 33, 36, 40, 47, 58]
- B. [19, 32, 25, 47, 58, 33, 40, 36]
- C. [33, 32, 25, 19, 36, 47, 40, 58]
- D. [33, 32, 25, 19, 36, 47, 58, 40]
Correct answer: C. [33, 32, 25, 19, 36, 47, 40, 58]
Smaller elements are swapped forward in scan order (so their relative order is kept here, but the larger ones get shuffled). The pivot 36 lands at index 4, its final sorted position: [33, 32, 25, 19, 36, 47, 40, 58].
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 47, 74, 63, 43, 45, 59, 85 are inserted in this order into an empty table of size 13 using quadratic probing: probe i goes to (k mod…
- What does this loop-counting code print? (Python) python n, c = 300, 0 i = 2 while i <= n: c += 1 i = i i print(c)
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 1000, 0, 1000, 0…
- A mountain is a contiguous subarray of length ≥ 3 that strictly increases to a single peak and then strictly decreases. What is the length…
- How many inversions (pairs i < j with a[i] > a[j]) does [38, 33, 1, 17, 31, 28, 18, 35] have? (This is also the number of swaps bubble…
- What is the worst-case number of comparisons made by binary search (three-way comparison per probe) on a sorted array of 1,000 elements…