Created
April 28, 2017 20:39
-
-
Save thinkphp/f5066c37d51e8afc61934fafccd297ce to your computer and use it in GitHub Desktop.
This function computes max of a,b using Divide Et Impera with the var passed by reference.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
int max2(int a, int b) { | |
if(a > b) return a;else return b; | |
}; | |
int dei(int arr[], int lo, int hi,int* max) { | |
if(lo == hi) { | |
return arr[lo]; | |
} else { | |
int a = dei(arr,lo,(lo+hi)/2,max); | |
int b = dei(arr,(lo+hi)/2+1,hi,max); | |
*max = max2(a,b); | |
} | |
}; | |
int main() { | |
int max; | |
int arr[10] = {0,1,2,3,91,5,6,7,8,4}; | |
dei(arr,0,9,&max); | |
printf("%d", max); | |
return(0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment