- (void)viewDidLoad {
[super viewDidLoad];
int number[10] = {1,2,3,4,5,6,7,8,9,10};
int num = (int)sizeof(number) / sizeof(int);
int snum = 7;
[self search:number min:0 max:num searchNum:snum];
}
// 递归调用
- (void)search:(int *)number min:(int)min max:(int)max searchNum:(int)snum {
int mid = (min + max) / 2;
if (number[mid] == snum ) {
NSLog(@"%d",mid);
}else if (number[mid] > snum) {
max = mid-1;
[self search:number min:min max:max searchNum:snum];
}else if (number[mid] < snum) {
min = mid+1;
[self search:number min:min max:max searchNum:snum];
}
}
网友评论