A2 Write a program to sort an array in ascending order

    class Program
    {
        static void Main(string[] args)
        {
            int[] a = new int[5];
            int temp;
            Console.WriteLine("Enter 5 Numbers : ");
            for (int i = 0; i < a.Length; i++)
                a[i] = int.Parse(Console.ReadLine());

            for (int i = 0; i < a.Length - 1; i++)
            {
                for (int j = i; j < a.Length; j++)
                {
                    if (a[i] > a[j])
                    {
                        temp = a[i];
                        a[i] = a[j];
                        a[j] = temp;
                    }
                }
            }
            // Array.Sort(a); Inbuilt Function

            Console.WriteLine("\nSorted Array : ");
            for (int i = 0; i < a.Length; i++)
                Console.WriteLine(a[i]);

            Console.ReadLine();
        }

    }

Output : 




1 comment: