Recursion : Searching an array

aj.wh.ca 0 Tallied Votes 147 Views Share

Find the maximum element in an unsorted array using recursion

int max(int *array, int len)
{
  int n1,n2;
  if(len == 1) return array[0] ;


  n1 = max(array , len/2);
  n2 = max(array + len/2 , len - len/2) ;
  return (n1 > n2 ? n1 : n2) ;
}


int main()
{
   int A[] = {2,7,91,8,9,11} ;
   int B[] = {2,7,91,8,9} ;

   printf("\n%d ",max(&A[0], 6));
   printf("\n%d\n ",max(&B[0], 5));
}
smma_abbas 0 Newbie Poster

Nice to see a good use of Divide and conquer programming

sweetleaf 0 Newbie Poster

dude u do not need to pass &a[0] as parameter simply the name of the array serves as the address of the first element

sweetleaf 0 Newbie Poster

good program though

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.