Quick actions

cmd+k|ctrl+k

Navigation

Languages

Udemy: Master Coding Interview - Bubble Sort - #165

Snippet info

Language

Csharp

Visibility

public

Author

jcurrie33

Created

2020-08-12T07:26:59Z

Updated

2020-08-12T07:26:59Z

using System;

class MainClass {
    static void Main() {
        int[] numbers = {99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0};
        Console.WriteLine(ToString(numbers));
        BubbleSort(numbers);
        Console.WriteLine(ToString(numbers));
    }
    
    public static void BubbleSort(int[] array) {
        for (int i = 0; i < array.Length; i++) {
            for (int j = 0; j < array.Length - 1 - i; j++) {
                if (array[j] > array[j+1]) {
                    int temp = array[j+1];
                    array[j+1] = array[j];
                    array[j] = 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