The Gateway to Computer Science Excellence
First time here? Checkout the FAQ!
+18 votes

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 Active (3.3k points) | 6k 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??
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
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 


@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..   :)


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 .

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

I want insert d between a and b....

key =d
Now it is a,d,b,c
Here I did something like movement.  You have seen such movements in insertion sort.
thank u:)
So which version of insertion sort do we have to consider :1st or 2nd (according to wiki) for exams ?
ISRO has a pattern of asking every ambiguous previous year GATE question lol
In place algo i.e space O(1) but for this quick sort not applicable as it takes O(n)

am i correct?plz suggest any1.
in insertion sort we do half exchanges, i think they are counted as swaps in this question.

4 Answers

+21 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 (411 points)
edited 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) 


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


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 :

@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.


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

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 ?

Sir in worst case, number of levels in quick sort is n .

Number of swaps in 1st level = n-1. In 2nd level n-2  and so on.

Total swaps= (n-1)+(n-2)+....+1=n(n-1)/2

= no of inversions
+10 votes
(C) Selection sort
answered by Loyal (5.8k points)
+5 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.8k 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)
+3 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 by Loyal (9.7k points)

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
49,541 questions
54,084 answers
70,994 users