How to find largest and smallest No in unsorted array




How to find out the min & max form an unsorted array

Logic:

Suppose an Array value 11,1,23,13,7

Step 1:

Put array first value in min and max variable
Min = 11
Max = 11

Step 2:

Through loop compare min and min value with I variable

If( Min > array index value) then
Min = array index value
Else
Max = array index value





 public static string maxAndMin(int[] arr)

        {
            int min = arr[0];
            int max = arr[0];
            // 11 1 13 25 17
            for (int i = 1; i <= arr.Length - 2; i++)
            {
                   if (arr[i] > max)
                    {
                        
                         max = arr[i];
                      
                    }
                    else
                    {
                       
                        min = arr[i];
                      
                    }
            }
            return min +" " +max;
        }



static void Main(string[] args)
        {
            Console.WriteLine("**********************************************************************");
            Console.WriteLine("How to find largest and smallest No in unsorted array ");
            Console.WriteLine("**********************************************************************");

            Console.WriteLine("Enter the size of array");
            int ArraySize= Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("Enter the array element");
            int[] arr = new int[ArraySize];
            for (int i = 0; i<= ArraySize - 1; i++)
            {
                arr[i]= Convert.ToInt32(Console.ReadLine());
            }
            Console.WriteLine(" Result  " + Program.maxAndMin(arr));
          
            Console.ReadLine();

        }


OutPut 

**********************************************************************

How to find largest and smallest No in unsorted array
**********************************************************************
Enter the size of array
5
Enter the array element
11
1
13
25
23

Result  1 25



Share:

No comments:

Post a Comment

Popular

Tags

Mobile

Recent Posts