Monday 10 December 2012

Code for Bubble Sort

using System;
namespace Console_BubbleSort
{
    class BubbleSort
    {
        public void dispBS()
        {
            int[] a = { 4, 6, 9, 83, 34, 45 };
            int temp;

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

                }

            }

            Console.WriteLine("The Sorted array");
            foreach (int aa in a)
                Console.Write(aa + " ");
        }
        static void Main(string[] args)
        {
            BubbleSort bs = new BubbleSort();
            bs.dispBS();
            Console.ReadKey();
        }
    }
}

No comments:

Post a Comment