Untitled

mail@pastecode.io avatar
unknown
plain_text
6 months ago
1.1 kB
0
Indexable
Never
using System;

class BubbleSortExample
{
    static void Main()
    {
        int[] arr = { 64, 34, 25, 12, 22, 11, 90, 87, 56, 43, 76, 88, 99, 33, 45, 77, 79, 80, 91, 10 };
        
        Console.WriteLine("Несортиран масив:");
        PrintArray(arr);

        BubbleSort(arr);

        Console.WriteLine("\nСортиран масив:");
        PrintArray(arr);
    }

    static void BubbleSort(int[] array)
    {
        int n = array.Length;

        for (int i = 0; i < n - 1; i++)
        {
            for (int j = 0; j < n - i - 1; j++)
            {
                if (array[j] > array[j + 1])
                {
                    // Размяна на елементите
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                }
            }
        }
    }

    static void PrintArray(int[] array)
    {
        foreach (int element in array)
        {
            Console.Write(element + " ");
        }
        Console.WriteLine();
    }
}