//非递归算法查找未使用二分法
int Max(int a[], int n){
int max_ret;
int i;
max_ret = a[0];
for(i=0; i if(max_ret < a[i]) max_ret = a[i];
}
return max_ret;
}
//二分法的递归算法实现
void Max_digui(int a[], int i, int j, int *max){
int middle;
int max1;
if(i==j) *max = a[i];//递归结束条件
else{
if((i+1) == j){
if(a[i] > a[j]) *max = a[i];
else *max = a[j];
}else{
middle = (i + j)/2;
Max_digui(a, i, middle, &max1);
Max_digui(a, middle+1, j,max);
if(max1 > *max) *max = max1;
}
}
}
int main(int argc, char* argv[])
{
int a[] = {1, 4, 15, 2, 27, 27, 12, 14, 27};
int max;
max = Max(a, 9);
printf("非递归%d\n", max);
Max_digui(a, 0, 8, &max);
printf("递归:%d\n", max);
printf("Hello World!\n");
return 0;
}
阅读(3742) | 评论(1) | 转发(0) |