Awesome q2a theme
0 votes
Can anybody explain me why the number of swaps in selection sort algorithm is  (n-1) in worst case ?

Let's say the array is {6,5,4,3,2,1} then the number of swaps should be 5. But I think the no of swaps should be 3.

Please explain.
in Algorithms by (7 points) | 35 views
{3,4,6,5,1,2} - check this
Is there any formula to calculate number of swaps in selection sort algo??
@Akshay0798 n-1 :)

1 Answer

+1 vote

Basic selection sort procedure works as follow:

  1. Find ith minimum from the array.
  2. Place it at ith position.

Even if you have the ith minimum at ith position already, we do a swap, so n-1 swaps for all permutations.

by (3.2k 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
Welcome to GATE CSE Doubts, where you can ask questions and receive answers from other members of the community.
9,105 questions
3,156 answers
95,955 users