Algorithm - binary search algorithm (OC, Swift, Python)

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/u014220518/article/details/102629188

Foreword

Binary search is very common algorithm in the program development process, but also the programmer interview process knowledge point inspection algorithms process the most frequently asked of knowledge; binary search in the actual development process often used to; it is such in order to find a one-dimensional array of a maximum number; and all we can contrast the middle of the array elements each, and then narrow down your search.

Binary search is a very fast and efficient search algorithm, because every time to find data to find space will be reduced to half the length of the array of principle, until you find an empty space before the end of the search.

But binary search is an ordered array of targeted, but that does not change often array; and if there is less data than the data, nor is it geared to finding a half, after all traversed once is enough, relative to the amount of data processing when comparing large arrays, the advantages of binary search is more obvious!

Code

Here I put OC, Swift and Python binary codes are looking for, so we learn from the exchange.

OC

- (NSInteger)halveSearch:(NSArray *)array target:(id)target{
    if(array.count == 0){
        return -1;
    }
    //标记区间的开始小标
    NSInteger start = 0;
    //标记区间结束的下标
    NSInteger end = array.count - 1;
    //标记区间中间的下标
    NSInteger middle = 0;
    
    while (start < end - 1) {
        //取区间数组里面的中间下标
        middle = start + (end - start) / 2;
        
        //中间值大于目标值
        if([array[middle] integerValue] > [target integerValue]){
            //目标值在中间值前面,将中间值赋给end作为最后一个值,在前面进行搜索取值
            end = middle;
        }else{
            //中间值小于目标值
            //将中间值赋给start作为开始值,在后面一段进行搜索
            start =  middle;
        }
        
        //如果起始值等于目标值
        if ([array[start] integerValue] == [target integerValue]) {
            return start;
        }
        
        //如果结束值等于目标值
        if ([array[end] integerValue] == [target integerValue]) {
            return end;
        }
    }
    
    return -1;
}

Swift

func halveSearch(_ array: Array<Any>, target: NSInteger) -> NSInteger {
        if array.count == 0{
            return -1
        }
        //标记区间的开始小标
        var start = 0
        //标记区间结束的下标
        var end = array.count - 1
        //标记区间中间的下标
        var middle = 0
        
        while start < end - 1 {
            //取区间数组里面的中间下标
            middle = start + (end - start) / 2
            
            //中间值大于目标值
            if (array[middle] as AnyObject).integerValue > target{
                //目标值在中间值前面,将中间值赋给end作为最后一个值,在前面进行搜索取值
                end = middle
            }else{
                //中间值小于目标值
                //将中间值赋给start作为开始值,在后面一段进行搜索
                start =  middle
            }
            
            //如果起始值等于目标值
            if (array[start] as AnyObject).integerValue == target{
                return start
            }
            
            //如果结束值等于目标值
            if (array[end] as AnyObject).integerValue == target{
                return end
            }
        }
        
        return -1
    }

Python

def halveSearch(array, target):
	if len(array) == 0:
		return -1

	# 标记区间的开始小标
	start = 0
	# 标记区间结束的下标
	end = len(array) - 1
	# 标记区间中间的下标
	middle = 0

	while start < end - 1:
		# 取区间数组里面的中间下标
		middle = int(start + (end - start) / 2)

		# 中间值大于目标值
		if array[middle] > target:
			# 目标值在中间值前面,将中间值赋给end作为最后一个值,在前面进行搜索取值
			end = middle
		else:
			# 中间值小于目标值
            # 将中间值赋给start作为开始值,在后面一段进行搜索
			start =  middle

		# 如果起始值等于目标值
		if array[start] == target:
			return start

		# 如果结束值等于目标值
		if array[end] == target:
			return end

	return -1

Conclusion

Great God welcome you to their valuable comments and suggestions, we welcome into the group exchange 365 152 048!

CSDN blog https://zfj1128.blog.csdn.net
GITEE Home https://gitee.com/zfj1128
GITHUB Home https://github.com/zfjsyqk

Guess you like

Origin blog.csdn.net/u014220518/article/details/102629188