• edited by
21,274 views
51 51 votes

An array contains four occurrences of $0$, five occurrences of $1$, and three occurrences of $2$ in any order. The array is to be sorted using swap operations (elements that are swapped need to be adjacent).

  1.  What is the minimum number of swaps needed to sort such an array in the worst case?
  2.  Give an ordering of  elements in the above array so that the minimum number of swaps needed to sort the array is maximum.

7 Answers

Best answer
59 59 votes
Since swaps are needed to be of adjacent elements only, the algorithm is actually Bubble sort.

In bubble sort, all smaller elements to right of an element are required to be swapped. So, if have ordering

$[2,2,2,1,1,1,1,1,0,0,0,0]$, then we need total $47$ swaps, and this will be the worst case.

So, it answers actually both parts.
• edited by
50 50 votes

Reference : https://discuss.codechef.com/questions/64884/wprob-editorial

The number of swaps an algorithm performs is equal to the number of inversions present in the input array.

The question is somewhat directly taken from the exercise of coremen

I'll solve this later first question :P

(a) Minimum number of swaps in worst case

   This would occur when all the elements in the array would be present in descending order.

So, Number of inversions for 2=(9*3)=27

     Number of inversions for 1 = (4*5)=20

     0 would have no inversions.

So, total inversions=47

2 2 2 1 1 1 1 1 0 0 0 0

(b) The above configuration of the array will give the maximum number of swaps to sort the array.


Now coming to coremen exercise

We know insertion sort works by placing the unsorted element in it's correct position within sorted elements.

The running time of insertion sort depends on the number of inversions present in the array.

Assuming array has n distinct elements.

Case A : Array is sorted in ascending order

In this case, number of Inversions would be 0, so 0 swaps, however, a total of n-1 comparisons will still be made.

So, best case complexity comes out to be O(n).

Case B: Array is sorted in descending order

Total inversions come out to be 

(n-1)+(n-2)+(n-3)+.......1 = $\frac{n(n-1)}{2}$  =  O(n2).

This will be exactly equal to the number of swaps and comparisons insertion sort will perform in this case.

So, worst case complexity comes out to be O(n2).

15 15 votes

$a)$ Bubble sort

Source : https://en.wikipedia.org/wiki/Bubble_sort

 

$[2,2,\fbox{2},\underbrace{1,1,1,1,1,0,0,0,0}]$

$\downarrow$ After $9$ swaps

$[2,2,1,1,1,1,1,0,0,0,0,2]$

$\downarrow$ Similarly After $18$ swaps

$[1,1,1,1,\fbox{1},\underbrace{0,0,0,0},2,2,2]$

$\downarrow$ After $4$ swaps

$[1,1,1,1,0,0,0,0,1,2,2,2]$

$\downarrow$ Similarly After $16$ swaps

$[0,0,0,0,1,1,1,1,1,2,2,2]$

$9+9+9+4+4+4+4+4=47$ Swaps


$b)$

$[2,2,2,1,1,1,1,1,0,0,0,0]$

3 3 votes

elements that are swapped need to be adjacent

This screams bubble sort. The worst case would be when the input is sorted in descending order.

$\left \{ 2,2,2,1,1,1,1,1,0,0,0,0 \right \}$

 

No of swaps = Number of inversions.

For all three of the 2's, all others are inversions => $15+12$

For all five of the 1's => $20$ (count just with 0, because 2 already covered)

For all the 0's, inversions are already covered.

Total inversions = $20+15+12=47$

Hence, swaps required = $47$

0 0 votes

Notes : 

 

  •  If a problem mentions minimizing swaps, the answer is likely Selection Sort .
  •  If a problem mentions nearly sorted data or small number of inversions , the answer is Insertion Sort.
  •  If the complexity must be $O(n^2)$ regardless of the input, you are dealing with Selection Sort

 

FeatureBubble SortInsertion SortSelection Sort
LogicAdjacent Swaps Shifting for Key Finding Global Min
Best Case Time$O(n)$$O(n)$$O(n^2)$
Worst Case Time$O(n^2)$$O(n^2)$$O(n^2)$
Max Swaps$n(n-1)/2$$0$ (uses shifts)$n - 1$
Adaptive?Yes (with flag)YesNo
Stable?YesYesNo
Position:
Show:

Related questions

51 51 votes
8 answers 8 answers
17.6k
17.6k views
Kathleen asked Sep 14, 2014
17,572 views
A multiset is an unordered collection of elements where elements may repeat any number of times. The size of a multiset is the number of elements in it, counting repetiti...
107 107 votes
13 answers 13 answers
38.9k
38.9k views
Kathleen asked Sep 14, 2014
38,892 views
Consider the following functions$f(n) = 3n^{\sqrt{n}}$$g(n) = 2^{\sqrt{n}{\log_{2}n}}$$h(n) = n!$Which of the following is true?$h(n)$ is $O(f(n))$$h(n)$ is $O(g(n))$$g(n...
41 41 votes
5 answers 5 answers
10.2k
10.2k views
Kathleen asked Sep 14, 2014
10,238 views
A recursive program to compute Fibonacci numbers is shown below. Assume you are also given an array $f [ 0\ldots m]$ with all elements initialized to $0.$fib(n) { if (n ...
14 14 votes
2 2 answers
4.4k
4.4k views
Kathleen asked Sep 14, 2014
4,391 views
Consider a bank database with only one relation transaction (transno, acctno, date, amount)The amount attribute value is positive for deposits and negative for withdrawa...