美文网首页
折半查找

折半查找

作者: 千秋画雪 | 来源:发表于2016-10-28 23:25 被阅读10次
    - (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];
       }
    
    }
    

    相关文章

      网友评论

          本文标题:折半查找

          本文链接:https://www.haomeiwen.com/subject/qpbeuttx.html