edited by
13,294 views

7 Answers

Best answer
20 20 votes
Answer is 10. All swaps are in following order:

8,7,22,9,31,5,13

8,7,9,22,31,5,13

8,7,9,22,5,31,13

8,7,9,22,5,13,31

7,8,9,22,5,13,31

7,8,9,5,22,13,31

7,8,9,5,13,22,31

7,8,5,9,13,22,31

7,5,8,9,13,22,31

5,7,8,9,13,22,31
5 5 votes

Ans.(D)

In Bubble sort, largest element moves to right. So a swapping is done, when a smaller element is found on right side.

So to count number of swaps for an element, just count number of elements on right side which are smaller than it.

Array is [8, 22, 7, 9, 31, 5, 13].
Number of elements smaller than 8 on right side:- 2

Number of elements smaller than 22 on right side:- 4

Number of elements smaller than 7 on right side:- 1

Number of elements smaller than 9 on right side:- 1

Number of elements smaller than 31 on right side:- 2

Number of elements smaller than 5 on right side:- 0

Number of elements smaller than 13 on right side:- 0

 

Total number of swappings required is : 10

Credits:

https://gateoverflow.in/14398/number-swappings-bubble-least-possible-shortcut-available?show=14400#a14400

Answer:
Position:
Show:

Related questions

8 8 votes
1 answers 1 answer
7.4k
7.4k views
sh!va asked May 7, 2017
7,389 views
Estimation at software development effort for organic software in basic COCOMO is:E = 2.0 (KLOC) 1.05 PME = 3.4 (KLOC) 1.06 PME = 2.4 (KLOC) 1.05 PME = 2.4 (KLOC) 1.07...
12 12 votes
6 answers 6 answers
8.4k
8.4k views
sh!va asked May 7, 2017
8,364 views
Which one of the following in-place sorting algorithms needs the minimum number of swaps?Insertion SortQuick SortHeap SortSelection Sort
9 9 votes
5 answers 5 answers
8.9k
8.9k views
go_editor asked Sep 28, 2014
8,871 views
In the context of modular software design, which one of the following combinations is desirable?High cohesion and high couplingHigh cohesion and low couplingLow cohesion ...
16 16 votes
3 answers 3 answers
5.3k
5.3k views
sh!va asked May 7, 2017
5,250 views
The time complexity of computing the transitive closure of a binary relation on a set of $n$ elements is known to bea. $O(n\log n)$b. $O\left( n^{3/2}\right)$c. $O( n^3 )...