Selection Sort in C#

Published on 30 December 2018 (Updated: 13 May 2026)

Welcome to the Selection Sort in C# page! Here, you'll find the source code for this program as well as a description of how the program works.

Current Solution

using System.Collections.Generic;
using System.Runtime.InteropServices;

if (args is not [var input] || !TryParseList(input.AsSpan(), out var numbers))
    return ExitWithUsage();

SelectionSort(numbers);

Console.WriteLine(string.Join(", ", numbers));
return 0;

static bool TryParseList(ReadOnlySpan<char> span, out List<int> numbers)
{
    numbers = new(span.Count(',') + 1);

    while (!span.IsEmpty)
    {
        int comma = span.IndexOf(',');
        var token = comma >= 0 ? span[..comma] : span;

        span = comma >= 0 ? span[(comma + 1)..] : [];

        if (!int.TryParse(token, out int n))
            return false;

        numbers.Add(n);
    }

    return numbers.Count > 1;
}

static void SelectionSort(List<int> list)
{
    if (list.Count < 2)
        return;

    Span<int> span = CollectionsMarshal.AsSpan(list);

    int n = span.Length;

    for (int i = 0; i < n - 1; i++)
    {
        int minIndex = i;

        for (int j = i + 1; j < n; j++)
        {
            if (span[j] < span[minIndex])
                minIndex = j;
        }

        if (minIndex != i)
            (span[i], span[minIndex]) = (span[minIndex], span[i]);
    }
}

static int ExitWithUsage()
{
    Console.WriteLine(
        "Usage: please provide a list of at least two integers to sort in the format \"1, 2, 3, 4, 5\""
    );
    return 1;
}

Selection Sort in C# was written by:

If you see anything you'd like to change or update, please consider contributing.

How to Implement the Solution

No 'How to Implement the Solution' section available. Please consider contributing.

How to Run the Solution

No 'How to Run the Solution' section available. Please consider contributing.