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 [49, 52, 21, 37, 35, 19, 9, 44]. What does the array look like after pass 2, and how many swaps have been made so far?
- A. [21, 35, 19, 9, 37, 44, 49, 52]; 12 swaps
- B. [21, 37, 35, 19, 9, 44, 49, 52]; 12 swaps
- C. [49, 21, 37, 35, 19, 9, 44, 52]; 12 swaps
- D. [9, 19, 21, 37, 35, 52, 49, 44]; 12 swaps
Correct answer: B. [21, 37, 35, 19, 9, 44, 49, 52]; 12 swaps
Each pass bubbles the largest remaining element to the end. After 2 passes the last 2 positions are final: [21, 37, 35, 19, 9, 44, 49, 52], using 12 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
- Z-array: z[i] is the length of the longest common prefix of s and s[i:], with z[0] defined as 0. What is printed? python s = "aaaaabaaa" z…
- 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][1] = 5…
- An array of size 9 holds the numbers 1..9, except that one number is missing and one number appears twice: [6, 5, 1, 3, 4, 7, 2, 8, 2]…
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "aababaaabbba", "aba" over = sum(1 for i in…
- Bars of width 1 have heights [2, 3, 1, 5, 2, 1, 1, 6]. What is the area of the largest rectangle that fits under the histogram (computed…
- For an array of n elements, how many contiguous subarrays are there, and how many subsequences (including the empty one)?