Tuesday, 7 March 2017

UGC NET Algorithm Solved Question

Q:: What is the maximum number of comparisons to find minimum and maximum element from an array with n elements ?

(A) 2n-2                                                            (B) 2n

(C) 1.5n-2                                                         (D) n

Answer :: (C)

Explanation:: 

Let us take some array elements as 12, 34, 5, 66, 22, 43, 7, 9

First of all we will compare first and second element and keep the larger one as max and smaller as min, then we take next two elements and compare them and then compare the larger with max and smaller with min like this we have 3 comparison for every two elements So 1.5 for each element making a total of 1.5n but for the first two we only make one comparison so finally no. of comparison become 1.5n-2.
So, (C) is correct.

C Program to find Maximum and Minimum element from array

#include<stdio.h>
#include<conio.h>
void main()
{
int i,min,a[100],max,n;
clrscr();
printf("Enter no. of elements to be stored");
scanf("%d",&n);
for(i=0;i<n;i=i+2)
{
printf("Enter no.\n");
scanf("%d",&a[i]);
}
if(a[0]>a[1])
{
max=a[0];min=a[1];
}
else
{
max=a[1];min=a[0];
}

for(i=2;i<=n-2;i++)
{
    if(a[i]>a[i+1])
    {
if(a[i]>max)
max=a[i];
if(a[i+1]<min)
min=a[i+1];
    }
    else
    {
if(a[i]<min)
min=a[i];
if(a[i+1]>max)
max=a[i+1];
    }

}

printf("\nMAX = %d\nMIN = %d",max,min);
getch();
}












C Program to find Maximum and Minimum element from array, complexity of finding Maximum and Minimum element from array, max and min element from array, best algorithm to find Maximum and Minimum element from array

No comments:

Post a Comment