0% found this document useful (0 votes)
27 views3 pages

Analysis of Algorithms CS 477/677 Homework 3: Ashutosh Singandhupe September 27, 2016

This document contains code for finding the maximum element in an array using recursion and binary search. It defines a findmax function that takes an array, low index, high index, and size as parameters. It recursively calls itself to find the maximum in the left and right halves and returns the larger of the two. Main calls findmax on a sample array and prints the index of the maximum element. It also contains code to segregate positive and negative numbers in an array in-place.

Uploaded by

Ashutosh Ashu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views3 pages

Analysis of Algorithms CS 477/677 Homework 3: Ashutosh Singandhupe September 27, 2016

This document contains code for finding the maximum element in an array using recursion and binary search. It defines a findmax function that takes an array, low index, high index, and size as parameters. It recursively calls itself to find the maximum in the left and right halves and returns the larger of the two. Main calls findmax on a sample array and prints the index of the maximum element. It also contains code to segregate positive and negative numbers in an array in-place.

Uploaded by

Ashutosh Ashu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Analysis of Algorithms CS 477/677

Homework 3
Ashutosh Singandhupe
September 27, 2016

1
1 a) Code
#include<s t d i o . h>

i n t f i n d m a x ( i n t a r r [ ] , i n t low , i n t high , i n t n )
{

i f ( low==h i g h ) return low ;


else
{
i n t mid = low + ( highlow ) / 2 ;
// p r i n t f (\ n low=%d mid=%d h i g h=%d , low , mid , h i g h ) ;
i n t temp1=f i n d m a x ( a r r , low , mid , n ) ;
// p r i n t f (\ ntemp1=%d , temp1 ) ;
i n t temp2=f i n d m a x ( a r r , mid+1 , high , n ) ;
// p r i n t f (\ n temp1=%d temp2=%d , temp1 , temp2 ) ;
i f ( a r r [ temp1]>= a r r [ temp2 ] )
return temp1 ;
e l s e return temp2 ;
}
}

int call max ( int a r r [ ] , int n )


{
return f i n d m a x ( a r r , 0 , n1 , n ) ;
}

i n t main ( )
{

int a r r [ ] = { 1 , 4 , 9 , 3 , 4 , 9 , 5 , 6 , 9 , 3 , 7 } ;
int n = sizeof ( a r r )/ sizeof ( a r r [ 0 ] ) ;
p r i n t f ( \nN=%d , n ) ;
p r i n t f ( Index o f t h e max v a l u e i s %d , c a l l m a x ( a r r , n ) ) ;
return 0 ;
}

Output: Index of the max value is 2

2
2
3 3)
#include <s t d i o . h>

i n t main ( )
{

i n t a r r [ ] = { 4 , 3, 9 , 8 , 7 , 4, 2, 1, 0 , 6 , 5};
int n = sizeof ( a r r )/ sizeof ( a r r [ 0 ] ) ;
i n t i =0 , j=n1;
while ( i <j )
{
i f ( a r r [ i ] <0) i ++;
else
{
i n t temp=a r r [ i ] ;
a r r [ i ]= a r r [ j ] ;
a r r [ j ]=temp ;
j ;
}
}

p r i n t f ( Output \n ) ;
f o r ( i n t i =0; i <n ; i ++) p r i n t f ( %d , a r r [ i ] ) ;

return 0 ;
}

OUTPUT: -5 -3 -1 -2 -4 7 8 0 6 9 4

You might also like