Bubble Sort

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Graph
{
    class BubbleSort
    {
        static void Main1(string[] args)
        {

            int n = Convert.ToInt32(Console.ReadLine());
            string[] a_temp = Console.ReadLine().Split(' ');
            int[] a = Array.ConvertAll(a_temp, Int32.Parse);


            int Swap = 0;

            for (int i = 0; i < a.Length - 1; i++)
            {
                for (int j = 0; j < a.Length - 1; j++)
                {
                    if (a[j] > a[j+1])
                    {
                        int temp = 0;
                        temp = a[j];
                        a[j] = a[j+1];
                        a[j+1] = a[j];

                        Swap = Swap + 1;
                    }
                }
            }

            Console.WriteLine("Array is sorted in " + Swap + " swaps");
            Console.WriteLine("First Element: " + a[0]);
            Console.WriteLine("Last Element: " + a[a.Length - 1]);
            Console.ReadLine();
        }
    }

}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6