Bubble sort algorithm

Write a program to demonstrate Bubble sort algorithm?

Solution:

For more information about Bubble Sort Read the article

using System;

namespace Sorting
{
    class BubbleSort
    {
        static void Main()
        {
            int[] arr = { 6, 1, 3, 2, 9 };
            for (var j = 0; j <= arr.Length - 2; j++)
            {
                for (var i = 0; i <= arr.Length - 2; i++)
                {
                    if (arr[i] <= arr[i + 1]) continue;

                    var temp = arr[i + 1];
                    arr[i + 1] = arr[i];
                    arr[i] = temp;
                }
            }
            Console.WriteLine($"Sorted:{string.Join(" ", arr)}");
            Console.ReadLine();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *