Data Structures & AlgorithmsVery hardGATE CS · TCS NQT · Product Company SDE
Quicksort with Lomuto partitioning (last element as pivot) sorts [52, 89, 77, 41, 89, 97, 29, 94]. How many swaps happen in total, across all recursive calls?
- A. 8
- B. 6
- C. 7
- D. 5
Correct answer: B. 6
Each partition call swaps a[i] with a[j] whenever a smaller-or-equal element is found, plus the final pivot swap; summed over all recursive calls this totals 6 swaps.
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 singly linked list has nodes at positions 0..5 holding 38 -> 23 -> 89 -> 67 -> 63 -> 79, but the last node's next pointer points back to…
- Two nested loops iterate: the outer runs n times, and for each outer iteration the inner loop runs from 1 up to the current outer index i…
- What is the time complexity of binary searching for a value in a sorted array of n elements, then linearly scanning the next k elements…
- What does this code compute, and what is its time complexity for an array of n elements? c int best = a[0]; for (int i = 1; i < n; i++) if…
- Quicksort uses the Lomuto partition with the last element as pivot on [99, 24, 44, 86, 76, 60, 84, 3]. After one partition call, at which…
- What is the time complexity of this code, as a function of n? c for (int i = 0; i < n; i++) for (int j = i; j < n; j++) sum += a[i] a[j];