GATE Computer ScienceVery hardGATE CS
Bubble sort with early exit (the algorithm stops after a pass in which no swap occurs) sorts the array [6, 9, 3, 8, 0, 5, 2, 7, 1, 4] into ascending order. How many passes are performed, including the final pass that detects no swaps?
- A. 7
- B. 8
- C. 9
- D. 10
Correct answer: B. 8
In each pass an element moves left by at most one place, so the number of swapping passes is the largest number of bigger elements to the left of any element. Here that is 7 (for the element 1: 6, 9, 3, 8, 5, 2, 7). So 7 passes with swaps are needed, plus one final pass: 8.
Practise more Programming and Data Structures (GATE) questionsProgramming and Data Structures (GATE): notes and key points
More Programming and Data Structures (GATE) questions
- An LRU cache of capacity 3 (get and put both make a key the most recently used; put of an existing key updates its value; when a put adds…
- The standard nextpermutation algorithm (find the rightmost i with a[i] < a[i+1], swap a[i] with the rightmost larger a[j] to its right…
- A max-heap is stored in A[1..13] = [100, 60, 90, 50, 40, 85, 80, 20, 10, 30, 35, 70, 75]. The key at position 8 (the value 20) is deleted…
- Binary search (lo = 0, hi = n − 1, mid = ⌊(lo + hi)/2⌋; each loop iteration compares a[mid] once and ends the search on equality) is run…
- Evaluate the postfix expression 8 2 3 ^ / 2 3 + 5 3 - - where ^ is exponentiation (x ^ y = xʸ), / is integer division and the operators…
- How many structurally distinct binary search trees can be built from 7 distinct keys?