The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
x
+10 votes
3.3k views

Which one of the following in place sorting algorithms needs the minimum number of swaps?

  1. Quick sort
  2. Insertion sort
  3. Selection sort
  4. Heap sort
asked in Algorithms by Loyal (4.3k points) | 3.3k views
Shouldn't this be Insertion Sort?

As in Insertion Sort, we "technically" don't swap, but shift elements to place the element under consideration in proper place
does swap here means shifting the elements??
NU OF SWAP
IN BUBBLE SORT =O(n2)
in selection sort=n
in insertion sort =0
In insertion sort (implementation like Cormen) we should use the term Movement or Shifting.
Number of swapping:   We cant say it is zero. Swappings are done indirectly. (Just check initially 2nd element is swapped with first, other swaps are done indirectly)
 
It should not be asked actually.  Because we do not swap anything directly.
Why it is asked in GATE again in ISRO?
I think if they are asking about swapping in insertion sort, then they are following that approach of insertion sort where swapping is done.
See this https://cs.stackexchange.com/questions/21455/how-can-i-quantify-the-number-of-swaps-required-for-insertion-sort
This approach where swapping is done is more popular as insertion sort.. See wiki.
In wiki, they also have written insertion sort with approach2(Cormen) & recursive one. They also have written 2nd approach is little bit faster. But they have analysed the complexity based on 1st approach..

In case of Insertion sort ,

best case is O(n) swaps , when the array is already sorted .

worst case O(n2) swaps 

source:wikipedia

@Bikram sir..  as per the 1st approach where swapping is done..  Best case O(n) swap.. Worst case O(n sq) swap.

As per 2nd approach (like Cormen)..  best case 0 movements.. Worst case n(n-1)/2 movements..   :)

@ahwan

in worst case :

n(n-1)/2 = (n2 - n) /2 which is nothing but O(n2)  :)

Swaps in approach 1 & movements in approach 2 in worst case are O(n sq)..  but in best case swaps for approach 1 is O(n) where as no movements are done in approach 2. By the way key replaces the number with itself.. But it is not movement I guess..
yes it is swap , replacing key is same as swapping .
@Ahwan

where do the difference between swaps and movements?
@srestha
Swap: Exchange of values between two variables.
a,b
temp=a; a=b; b=temp;

Movement:
a,b,c,d  
I want insert d between a and b....

key =d
d=c;
c=b;
b=key
Now it is a,d,b,c
Here I did something like movement.  You have seen such movements in insertion sort.
thank u:)

4 Answers

+16 votes
Best answer
Selection sort

because in selection the maximum swaps which can take place are $O(n)$

because we pick up an element an find the minimum (in case of forward sorting) from the next index till the end of array and than perform the swap

hence $O(n)$ whereas in all other algos the swaps are greater ( considering Worst-Case scenario )
answered by (413 points)
edited ago by
Please compare number of swaps in selection sort with other sorting techniques given in above question.

Number of swaps : Worst case scenario 

  1. Quick sort = O(n2)
  2. Insertion sort = О(n2
  3. Selection sort = O(n)
  4. Heap sort = O(n logn) 

@Ahwan

In case of selection sort , number of swaps require :

Best case: No swaps required as all elements are properly arranged

Worst case: n-1 swaps required

Reference:

https://stackoverflow.com/questions/26688765/what-are-the-number-of-swaps-required-in-selection-sort-for-each-case

another source :

While selection sort is preferable to insertion sort in terms of number of writes

(Θ(n)  swaps versus Ο(n2) swaps),

That means selection sort has O(n) swaps while insertion sort have O(n2) swaps in worst case.

Reference :

https://en.wikipedia.org/wiki/Selection_sort#Comparison_to_other_sorting_algorithms

@bikram sir in selection sort, in best case also the number of swaps are O(n). In best case 1st smallest element will be A[1] itself and if we don't do any modification in the standard algorithm definition given in cormen then A[1] will be swapped with itself. Hence in each iteration element will be swapped with itself.

@vineet

In Best case , number of swaps are O(n) or 0 swaps , it really depends on the implementation .

And swapping with itself at first position only count as 0 swaps , as all elements are properly arranged .

What i read is in Best case number of swaps is 0 but it depends how selection sort is done..

for this gate question , no doubt answer is selection sort ..

You may read this answer , to get some idea https://stackoverflow.com/questions/26688765/what-are-the-number-of-swaps-required-in-selection-sort-for-each-case

yes sir it depends on implementation but, if we don't modify i.e if we write this check-> if(min!=j) then swap(A[min],A[j]) then there will be no swaps in best case. but if don't check and directly swap then in best also there will be O(n) swaps. But what i think is that algorithm without check should be considered as standard algorithm because in cormen it is simply mentioned "find first smallest element and swap it with A[1]......." now if A[1] itself is first smallest then also there should be swap even though it is not necessary.
partition (arr[], low, high)
{
    // pivot (Element to be placed at right position)
    pivot = arr[high];  
 
    i = (low - 1)  // Index of smaller element

for (j = low; j <= high- 1; j++) // no. of iterations=high-1-low+1
    {
        // If current element is smaller than or
        // equal to pivot
        if (arr[j] <= pivot)
        {
            i++;    // increment index of smaller element
            swap arr[i] and arr[j]
        }
    }
    swap arr[i + 1] and arr[high]) // +1 swap
    return (i + 1)
}

To understand my doubt please refer to the code above.

In the 1st level high=n and low=1. For the worst case of swapping, for each iteration there should be swapping. Hence total no. of swappings would be high-1-low+1=n-1-1+0=n-2 and outside the loop there is another swapping. Total swapping would be = n-1 in the 1st level. No. of swaps=O(n).

At 2nd level, if the pivot divides the array into equal halves then no. of swapping in this way is (low=1,high=(n/2)-1) so total swaps= (n/2)-1-1+1+1=n/2 and as there is another array set where low=n/2+1 and high=n so swaps=n/2 and hence total swaps=n.

If pivot element is placed at the end (for sorted array) then, low=1, high=n-1 so swaps= high-1-low+1+1=(n-1)-1-1+1+1=n-1.

We can assume that at each level the no. of swaps is O(n).

No. of levels= logn.

Total no. of swaps = nlogn.

How will it be n^2 @Bikram Sir ?

+9 votes
(C) Selection sort
answered by Boss (6.4k points)
+2 votes
Number of swaps
Quick sort = $\Theta (nlogn)$
Insertion sort =  $\Theta (n^{2})$
Selction sort = no swaps required since we are shifting elements in the array and not swapping
Heap sort =  $\Theta (nlogn)$

Option C
answered by Active (1.9k points)
in  quick  wont it be n^2 ?

Yes, for Quick Sort , worst case time complexity O(n2)

 on average, the algorithm takes O(n log n) comparisons to sort n items.

 

sir, in quisck sort, in worst case number of comparison will be O(n2) for dataset like 1,2,3,4,5...

but in worst case we have to do only nLogn swaps like 9,2,4,5,6,7,8

number of swaps will be like this n+(n/2+n/2)+(n/4+n/4+n/4+n/4) which is O(nlgn)
0 votes

Let's try to analyse the number of swaps in each of the given sorting algorithms. Quick sort – Worst Case input for maximum number of swaps will be already sorted array in decreasing order. Recurrence for Total number of swaps in this case : T(n) = T(n-1) + O(n) // O(n) swaps will occur in alternate calls to partition algorithm. = O(n2) Insertion sort - Worst Case input for maximum number of swaps will be already sorted array in ascending order.When a new element is inserted into an already sorted array of k size, it can lead to k swaps (in case it is the smallest of all) in worst case. For n-1 iterations of insertion sort, total swaps will be O(n2). Selection sort – There is no Worst case input for selection sort. Since it searches for the index of kth minimum element in kth iteration and then in one swap, it places that element into its orrect position. For n-1 iterations of selection sort, it can have O(n) swaps. Heap sort – Total number of swaps in Heap sort can be O(nlogn) as after performing Build-heap which may require O(n) swaps, it performs n-1 extract-min operations resulting into O(nlogn) swaps.

answered ago by Boss (6.1k points)


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

28,834 questions
36,688 answers
90,625 comments
34,641 users