Skip to content

Instantly share code, notes, and snippets.

@thinkphp
Created April 28, 2017 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thinkphp/f5066c37d51e8afc61934fafccd297ce to your computer and use it in GitHub Desktop.
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.
#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