Friday 17 August 2012

C program to find maximum and minimum of an array using Divide & Conquer rule

Today I am going to post a program that is going to find out the maximum and minimum element of an array. You will say that it is very easy. But here we are going to use the Divide & Conquer technique to solve this problem . This technique will be able to solve the problem in a much lesser time. Here is the code for you-

#include<stdio.h>

int a[50],max,min;

void find(int i,int n){
   int mid,max1,min1;

   if(i==n)
     max=min=a[i];
   else if(i==n-1)
     if(a[i]>=a[n-1]){
       max=a[n];
       min=a[i];
     }
     else{
       max=a[i];
       min=a[n];
     }
   else{
     mid=(i+n)/2;
     find(i,mid);
     max1=max;
     min1=min;
     find(mid+1,n);
     if(max<max1)
         max=max1;
     if(min>min1)
         min=min1;
   }


int main(){
   int i,n;

   printf("Enter size of array : ");
   scanf("%d",&n);

   printf("Enter elements in array-->\n");
   for(i=0;i<n;i++)
      scanf("%d",&a[i]);

   max=min=a[0];
   find(1,n-1);

   printf("Minimum : %d",min);
   printf("\nMaximum : %d",max);
  
   return 0;
}
NOTE : This code is compiled with GCC compiler under Linux environment Ubuntu 12.04 LTS Precise Pangolin.

21 comments:

  1. Replies
    1. madarchod!!! code galat hai.....bc

      Delete
    2. madarchod!!! code galat hai.....bc

      Delete
    3. madarchod!!! code galat hai.....bc

      Delete
    4. wrong code..plz update it

      Delete
    5. bhen k lode code galat hai..

      Delete
    6. randi ke bache code galat he bhsdk

      Delete
  2. NOTE : This code is compiled with GCC compiler under Linux environment Ubuntu 12.04 LTS Precise Pangolin.....


    CAN IT BE COMPILED ON TURBO C ????

    ReplyDelete
    Replies
    1. Ofcourse it can be compiled on turbo c just youll have to use something to hold the output screen.

      Delete
  3. little mistake: in main function, call find(0,n-1), not find(1,n-1)

    ReplyDelete
  4. this algo works only for assending orders

    ReplyDelete
    Replies
    1. no it is applicable to any input order

      Delete
    2. no you wrong..
      please enter 5 array element like..
      24
      56
      45
      26
      94...
      then you'll see that
      maximum = 95
      but
      minimum = 26(which should be 24)
      why...?

      Delete
    3. correct this:-
      in main function
      call find(0,n-1), not find(1,n-1)

      Delete
  5. Its working!!!!Just replace find(1,n-1) with find(0,n-1)...

    ReplyDelete
  6. Replies
    1. //Correction

      else if(i==n-1)
      {

      if(a[i]<=a[n])
      {
      max=a[n];
      min=a[i];
      }
      else{
      max=a[i];
      min=a[n];
      }

      Also change find(0,n-1)

      Delete
    2. Devru guru neenu

      Delete
  7. Reference to 'max' is ambigous

    ReplyDelete
  8. #include

    int a[50],max,min;
    void minMAX(int i, int j)
    {
    int min1,max1;
    if(i==j)
    min=max=a[i];
    else if(i==j-1)
    {
    if(a[i]>a[j])
    {
    max = a[i];
    min = a[j];
    }
    else
    {
    min = a[i];
    max = a[j];
    }
    }
    else
    {
    minMAX(i,(i+j)/2);
    min1 = min;
    max1 = max;
    minMAX((i+j)/2+1,j);
    if(min>min1)
    min=min1;
    if(max<max1)
    max=max1;
    }
    }
    void main(){
    int i,n;

    printf("Enter size of array : ");
    scanf("%d",&n);

    printf("Enter elements in array:");
    for(i=0;i<n;i++)
    scanf("%d",&a[i]);

    //max=min=a[0];
    minMAX(0,n-1);

    printf("Minimum : %d",min);
    printf("\nMaximum : %d",max);
    }

    ReplyDelete