Quick actions

cmd+k|ctrl+k

Navigation

Languages

Udemy: Master Coding Interview - Selection Sort - #168

Snippet info

Language

Csharp

Visibility

public

Author

jcurrie33

Created

2020-08-12T07:31:24Z

Updated

2020-08-12T07:38:50Z

using System;

class MainClass {
    static void Main() {
        int[] numbers = {99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0};
        Console.WriteLine(ToString(numbers));
        SelectionSort(numbers);
        Console.WriteLine(ToString(numbers));
    }
    
    public static void SelectionSort(int[] array) {
        for (int i = 0; i < array.Length; i++) {
            int smallestIndex = i;
            for (int j = i+1; j < array.Length; j++) {
                if (array[j] < array[smallestIndex]) {
                    smallestIndex = j;
                }
            }
            if (smallestIndex != i) {
                int temp = array[i];
                array[i] = array[smallestIndex];
                array[smallestIndex] = temp;
            }
        }
    }
    
    public static string ToString(int[] array) {
        string s = "{";
        for (int i = 0; i < array.Length; i++) {
            if (i > 0) {
                s += ", ";
            }
            s += array[i];
        }
        s += "}";
        return s;
    }
}
INFO