Awesome q2a theme
0 votes
33 views
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) | 33 views
0
{3,4,6,5,1,2} - check this
+1
Is there any formula to calculate number of swaps in selection sort algo??
0
@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,092 questions
3,152 answers
14,579 comments
95,935 users