Quick actions

cmd+k|ctrl+k

Navigation

Languages

Selection Sort C#

Snippet info

Language

Csharp

Visibility

public

Author

patrykstachowiak

Created

2025-02-26T14:24:00.864418Z

Updated

2025-02-26T15:15:31.39454Z

using System;

class SelectionSortExample {
    static void Main() {
        int[] numbers = { 99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0 };

        SelectionSort(numbers);

        Console.WriteLine("Sorted Array:");
        Console.WriteLine(string.Join(", ", numbers));
    }

    static void SelectionSort(int[] arr) {
        for (int i = 0; i < arr.Length - 1; i++) {
            int lowestValueIndex = i;
            
            for (int j = i + 1; j < arr.Length; j++) {
                if (arr[j] < arr[lowestValueIndex]) {
                    lowestValueIndex = j;
                }
            }
            
            int temp = arr[lowestValueIndex];
            arr[lowestValueIndex] = arr[i];
            arr[i] = temp;
        }
    }
}
INFO