37,771 views
52 52 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

5 Answers

Best answer
44 44 votes

Correct Option: C - 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 )

• edited by
10 10 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.

4 4 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
3 flags:
✌ Edit necessary (jayy_patel “wrong answer”)
✌ Low quality (rivyth “wrong answer”)
✌ Low quality (Akashsr3)
1 1 vote

Answer: Selection sort.


Explanation

  • Selection sort always performs exactly n−1 swaps (for an array of size nn):
    In each of the n−1 passes, it finds the minimum element in the unsorted portion and swaps it into place.
    Thus, regardless of the input’s initial order, it does exactly n−1 exchanges.

  • Insertion sort (when implemented via shifting instead of swapping) moves elements by “shifting” them one position at a time, which typically results in up to Θ(n^2) element moves in the worst case.
    If you instead implement insertion sort by repeatedly swapping adjacent elements until the new element reaches its correct spot, it can use Θ(n^2) swaps in the worst case (for example, if the input is reverse‐sorted).

  • Heap sort builds a heap in O(n) time and then repeatedly swaps the root with the last element and “heapifies” the root down. In the worst case, each of the n removals from the heap costs one swap to move the max element to the end plus up to log⁡n more swaps to restore the heap property—so on the order of Θ(nlog⁡n) swaps overall.

  • Quick sort (in its in‐place, two‐pointer partitioning form) also uses Θ(nlog⁡n) comparisons on average because when partition algorithm does n swaps atmost to place the pivot element at place and that recursive call is made logn times.( There are logn levels with each level summing size of n ) hence nlogn swaps at worst.

Because selection sort uses exactly n−1 swaps no matter what, while all of the other in-place algorithms here can require on the order of nlog⁡n or even n^2 swaps in the worst case, selection sort has the minimum (and tightly bounded) number of swaps.

1 flag:
✌ Edit necessary (Akashsr3 “Selection sort performs atmost n-1 swaps not exactly n-1 swaps”)
Answer:
Position:
Show:

Related questions

58 58 votes
5 answers 5 answers
61.7k
61.7k views
Rucha Shelke asked Sep 26, 2014
61,711 views
The median of $n$ elements can be found in $O(n)$ time. Which one of the following is correct about the complexity of quick sort, in which median is selected as pivot?$\T...
8 8 votes
6 6 answers
3.9k
3.9k views
Arjun asked Feb 27, 2025
3,887 views
Suppose that insertion sort is applied to the array $[1,3,5,7,9,11, x, 15,13]$ and it takes exactly two swaps to sort the array. Select all possible values of $x$.$10$$12...
18 18 votes
2 answers 2 answers
13.5k
13.5k views
Ishrat Jahan asked Nov 1, 2014
13,452 views
What is the bit rate of a video terminal unit with $80$ characters/line, $8$ $\text{bits/character}$ and horizontal sweep time of $100$ $\text{µs}$ (including $20$ $\text...
91 91 votes
10 answers 10 answers
45.1k
45.1k views
Rucha Shelke asked Sep 16, 2014
45,050 views
To implement Dijkstra’s shortest path algorithm on unweighted graphs so that it runs in linear time, the data structure to be used is:QueueStackHeapB-Tree