Data Structures & AlgorithmsHardGATE CS · DSA
Bubble sort (ascending; each pass compares adjacent pairs left to right and swaps if out of order) is applied to [3, 37, 10, 52, 30, 23, 22, 38]. What does the array look like after pass 2, and how many swaps have been made so far?
- A. [3, 10, 23, 22, 30, 37, 38, 52]; 8 swaps
- B. [3, 10, 37, 30, 23, 22, 38, 52]; 8 swaps
- C. [3, 10, 37, 52, 30, 23, 22, 38]; 8 swaps
- D. [3, 10, 30, 23, 22, 37, 38, 52]; 8 swaps
Correct answer: D. [3, 10, 30, 23, 22, 37, 38, 52]; 8 swaps
Each pass bubbles the largest remaining element to the end. After 2 passes the last 2 positions are final: [3, 10, 30, 23, 22, 37, 38, 52], using 8 swaps in total (the swap count equals the inversions removed so far).
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
- Records are sorted with a composite key. What is printed? python recs = [('anu', 80), ('zoya', 70), ('dev', 70), ('ravi', 70), ('meera'…
- Two sorted arrays of sizes m and n (m ≤ n) are given. What is the best possible time to find the median of their union?
- A circular queue in an array of size n uses front and rear indices and keeps one slot always empty. What is the maximum number of elements…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "bbaabbbabaaa", "aa" over = sum(1 for i in…
- A sorted array was rotated to give [35, 45, 52, 68, 78, 1, 5, 12, 18, 27, 28]. Binary search finds the index of the minimum: while lo <…
- The array [1, 2, 3, 4, 5, 6, 7, 8] is rotated RIGHT by 5 positions and then LEFT by 7 positions. What is the final array?