Quick actions

cmd+k|ctrl+k

Navigation

Languages

BubbleSort C#

Snippet info

Language

Csharp

Visibility

public

Author

patrykstachowiak

Created

2025-02-26T13:34:11.466197Z

Updated

2025-02-26T17:05:59.53369Z

using System;

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

        BubbleSort(numbers);

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

    static void BubbleSort(int[] arr) {
        bool swapped;

        for (int attempts = arr.Length - 1; attempts > 0; attempts--) {
            swapped = false;

            for (int i = 0; i < attempts; i++) {
                if (arr[i] > arr[i + 1]) {
                    int temp = arr[i];
                    arr[i] = arr[i + 1];
                    arr[i + 1] = temp;
                    swapped = true;
                }
            }

            if (!swapped) break;
        }
    }
}
INFO