using System;

class bubblesort

{

        static void Main(string[] args)

        {

            int[] sortarray = { 3, 2, 8, 4, 1 };  

            int t;

            Console.WriteLine("The Array is : ");

            for (int i = 0; i < sortarray.Length; i++)

            {

                Console.WriteLine(sortarray[i]);

            }

            for (int j = 0; j <= sortarray.Length - 2; j++)

            {

                for (int i = 0; i <= sortarray.Length - 2; i++)

                {

                    if (sortarray[i] > sortarray[i + 1])

                    {

                        t = sortarray[i + 1];

                        sortarray[i + 1] = sortarray[i];

                        sortarray[i] = t;

                    }

                }

            }

            Console.WriteLine("The Sorted Array :");

            foreach (int aray in a)                         

                Console.Write(aray + " ");

            Console.ReadLine();

        }

    }

Output of the C# Program:

The Array is: 3 2 8 4 1

The Sorted Array: 1 2 3 4 8

Your Answer
Sign In
Enter email and password to comment or answer
Not the answer you are looking for? Browse other questions tagged or ask your own question.