算法学习--二分查找法(golang版本)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/glw0223/article/details/88374116

说明

二分法查找,是一种在有序数组中查找特定元素的搜索算法。

其的时间复杂度O(logn)。

代码

这里使用了递归和非递归两种方式,其中func BinarySearch3的代码更简洁。

package binarySearch

import (
	"fmt"
)

//非递归
func BinarySearch1(sortedArray []int, key int)(result string){
	length:=len(sortedArray)
	if length <= 0{
		result = "有序数组异常"
	}else {
		if key < sortedArray[0]{
			result = "key 小于有序数组最小值"
		}else if key > sortedArray[length-1] {
			result = "key 大于有序数组最大值"
		}else {
			index:=length/2
			max:=length
			min:=0
			for(true){
				if key == sortedArray[index]{
					result = fmt.Sprint("成功找到,是有序数组的第 ",index, " 个")
					break
				}else if key < sortedArray[index] {
					max = index
				}else if key > sortedArray[index] {
					min = index
				}

				index = (max+min)/2
				if max == min || index == min || index == max {
					result = "没有找到"
					break
				}else {
					//fmt.Println(max, "  ", index, " ", min)
				}
			}
		}
	}

	return
}
//非递归
func BinarySearch3(sortedArray []int, key int)(result string){
	result="not find the key"
	max:=len(sortedArray)
	if max <= 0 {
		result = "有序数组异常"
	}else {
		min:=0
		max = max -1
		for min<=max {
			index:=(min+max)>>1
			if key<sortedArray[index]{
				max=index-1
			}else if key>sortedArray[index]{
				min=index+1
			}else {
				result=fmt.Sprintf("find it and the index is %d",index)
				break
			}
		}
	}
	return
}

//递归法
func BinarySearch2(sortedArray []int, min, max, key int) (result string){
	if len(sortedArray) <= 0 {
		result = "数组错误"
	}else {
		if key < sortedArray[0] {
			result = "小于最小值"
		}else if key > sortedArray[len(sortedArray)-1]{
			result = "大于最大值"
		}else {
			index:=((max+min)/2)
			temp:=sortedArray[index]
			if key == temp {
				result = fmt.Sprintf("找到啦!!! 是第%d个",index)
			}else if max == min || index == min || index == max {
				result = "没有找到"
			}else {
				result = ""

				if key < temp{
					max = index
				}else if key > temp {
					min = index
				}
				result=BinarySearch2(sortedArray, min, max, key)
			}
		}
	}

	return
}

测试代码

package test

import (
	"fmt"
	"github.com/glw0223/LeetCode-go/binarySearch"
	"testing"
)

func TestBinarySearch(t *testing.T)  {
	arr:=[]int{1,2,3,4,7,8,9}
	fmt.Println("------------------非递归测试------------------")
	key:=8
	result:=binarySearch.BinarySearch1(arr, key)
	fmt.Println(1,result)
	key=6
	result=binarySearch.BinarySearch1(arr, key)
	fmt.Println(2,result)
	key=10
	result=binarySearch.BinarySearch1(arr, key)
	fmt.Println(3,result)
	key=0
	result=binarySearch.BinarySearch1(arr, key)
	fmt.Println(4,result)

	fmt.Println("------------------递归测试------------------")
	key=8
	result=binarySearch.BinarySearch2(arr, 0, len(arr),key)
	fmt.Println(1,result)
	key=6
	result=binarySearch.BinarySearch2(arr, 0, len(arr),key)
	fmt.Println(2,result)
	key=10
	result=binarySearch.BinarySearch2(arr, 0, len(arr),key)
	fmt.Println(3,result)
	key=0
	result=binarySearch.BinarySearch2(arr, 0, len(arr),key)
	fmt.Println(4,result)
}

测试结构


API server listening at: 127.0.0.1:57899
=== RUN   TestBinarySearch
------------------非递归测试------------------
1 成功找到,是有序数组的第 5 个
2 没有找到
3 key 大于有序数组最大值
4 key 小于有序数组最小值
------------------递归测试------------------
1 找到啦!!! 是第5个
2 没有找到
3 大于最大值
4 小于最小值
--- PASS: TestBinarySearch (0.00s)
PASS

Debugger finished with exit code 0

github地址

https://github.com/glw0223/LeetCode-go
 

猜你喜欢

转载自blog.csdn.net/glw0223/article/details/88374116