Files
Go-http_framework/util/search/InsertionSearch.go
T

35 lines
581 B
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/*
* @Author : huangzj
* @Time : 2020/8/19 10:24
* @Description
*
*/
package search
func InsertionSearch(list []int, value int) int {
low := 0
high := len(list) - 1
var mid int
//插值查找这边如果数字和起始数字差别大的话,计算出来的下标会越界挺多的
if value > list[high-1] || value < list[0] {
return -1
}
for low < high {
mid = low + (high-low)*(value-list[low])/(list[high]-list[low])
if value == list[mid] {
return mid
}
if value < list[mid] {
high = mid - 1
} else {
low = mid + 1
}
}
return -1
}