US Trends

what is the maximum number of swaps that can be performed in the selection sort algorithm?

The maximum number of swaps performed by the standard selection sort algorithm on an array of nnn elements is n −1n-1n−1.

Quick Scoop: Core Idea

In the usual implementation of selection sort:

  1. You divide the array into a sorted part (at the front) and an unsorted part (the rest).
  1. On pass iii, you scan the unsorted part to find the minimum element’s index.
  2. At the end of that pass, you swap once : the element at position iii with that minimum element.

There are n−1n-1n−1 such passes (for i=0i=0i=0 to n−2n-2n−2), and each can perform at most one swap , so the maximum total is n−1n-1n−1 swaps.

Mini breakdown

  • Best case (already sorted):
    Many implementations check if the minimum is already at position iii; if so, they skip the swap → 0 swaps.
  • Worst case (max swaps):
    Each pass finds its minimum at a different position and swaps it with position iii, so every pass does one swap → n −1n-1n−1 swaps.
  • Average case (common result for standard variant):
    Often cited as about (n−1)/2(n-1)/2(n−1)/2 swaps, since roughly half the passes end up swapping when inputs are “random enough.”

A simple story version:
Imagine you’re lining up nnn books from shortest to tallest. Each step you look through the unsorted pile, pick the shortest book, and either leave it where it is (if it’s already in place) or swap it once into the next spot in the row. You never need more than one swap per step, and there are only n−1n-1n−1 such steps, so you can’t exceed n −1n-1n−1 swaps.

Tiny HTML table (as requested)

Below is an HTML table summarizing the swap counts for standard selection sort:

html

<table>
  <tr>
    <th>Case</th>
    <th>Number of swaps</th>
  </tr>
  <tr>
    <td>Best case (already sorted)</td>
    <td>0 swaps (if we skip swapping when element is already in place)[web:3][web:5]</td>
  </tr>
  <tr>
    <td>Worst case</td>
    <td>n - 1 swaps[web:1][web:2][web:3]</td>
  </tr>
  <tr>
    <td>Average case (typical statement)</td>
    <td>(n - 1) / 2 swaps[web:3]</td>
  </tr>
</table>

Information gathered from public forums or data available on the internet and portrayed here.

TL;DR: For the standard selection sort algorithm, the maximum number of swaps on nnn elements is n −1n-1n−1.