in GATE retagged by
788 views
6 votes
6 votes
  • A Multinational software vendor needs to choose two sorting algorithm implementations $S1$ and $S2$ to built a software for it's offshore clients.
  • $S1$ will be used in situations where item exchanges cost nothing but item comparisons remain expensive. Conversely, $S2$ will be used in situations where item comparisons cost nothing but item exchanges remain expensive.
  • Suppose the vendor can only use the insertion, selection, or bubble sorts for these implementations,  and suppose the vendor only cares about average-case asymptotic algorithmic complexity.

Which algorithm should the vendor use for each implementation?

  1.   Bubble sort for $S1$ and insertion sort for $S2$.
  2.   Insertion sort for $S1$ and bubble sort for $S2$.
  3.   Selection sort for $S1$ and insertion sort for $S2$.
  4.   Insertion sort for $S1$ and selection sort for $S2$.
in GATE retagged by
by
788 views

4 Comments

reshown by
pls provide explanation for this
1
1
@bikram sir plz explain little bit here i got confused
1
1

@rajan

Here we consider only average-case asymptotic algorithmic complexity. So here we have to compare average case running time of these 3 sorting algorithms.

For comparison insertion sort is better and for exchange selection sort .

see my answer below..

0
0

Selection sort is the best in case of number of swaps. It performs at most one swap per element.

Insertion sort takes $\frac{n^2}{4}$ comparisons in average case.

0
0

2 Answers

2 votes
2 votes
Best answer

Question says AVERAGE CASE time complexity only to consider .

Selection sort uses about (N/ 2 ) comparisons and N exchanges on average.

Insertion sort uses about ( N2/4) comparisons and (N/ 8) exchanges on average.

Bubble sort uses about (N/ 2) comparisons and ( N/ 2) exchanges on average.


So if exchanges cost nothing and only comparisons count, then insertion sort way better than the others.

If exchanges count but comparisons cost nothing, then selection sort beats the others  asymptotically.

Hence they need to use Insertion sort for S1 and Selection sort for S2.

selected by

2 Comments

can you provide some reference to this exchanges and comparison

caz in my notes I have something different
0
0

@Supremo

you can check with Algorithms, 4th Edition by Robert Sedgewick and Kevin Wayne .

0
0
1 vote
1 vote
Selection Sort for (S2) as we know has least number of items exchanges (order of N) ,Exchange happens only once while pushing the maximum towards end in every iteration . So we can use Selection sort where the item exchanges are expensive.

Insertion sort for (S1), is good when we know the comparisions are costly because it just tends to compare only the element at the end of sorted subarray. In the best case as we know it might even lead to O(n) comparisions.

4 Comments

@Bikram.

I got the comparisons part.

But why are you taking  "shifting" operation in insertion sort as "exchanges"? Shifting is different from exchanging right? So, there are 0 exchanges in insertion sort, right?
1
1

@Gokhale

This question demans exchanging ..

 I took both same because In most programming languages, creating space to temporarily store a variable is simply using another position on the stack and is essentially free. If your array elements are complex structures and you're unfortunate enough to be using a language where assignment involves copying the entire structure in memory, then shifting will be cheaper than swapping. But in either case, the big-O analysis will be the same; the overhead shows up as coefficients, not as increased asymptotic complexity.

so both shifting and swapping have O(n)  Average case performance.[1]

shifting is moving all elements by one position.

Swapping is exchanging the elements at two distinct indexes.

Reference:

[1] http://stackoverflow.com/questions/20224900/what-is-the-difference-between-swapping-and-shifting

0
0

@Bikram. 

I understand that there wont be any difference in average case time complexity. But I would have really appreciated if the sentence was:

insertion sort implemented by swapping

With this you leave no doubt.

For eg. No of multiplexers required in direct mapped cache.

Now, some might use decoder and get less no of multiplexers. Some might get the o/p with more number of multiplexers.

Point is there should be no space for ambiguity.

1
1
Answer:

Related questions

Quick search syntax
tags tag:apple
author user:martin
title title:apple
content content:apple
exclude -tag:apple
force match +apple
views views:100
score score:10
answers answers:2
is accepted isaccepted:true
is closed isclosed:true

64.3k questions

77.9k answers

244k comments

80.0k users