feat(Go-Tool):新增二分法查找算法及其单测、插入查询及其单测、lomuto划分查询第k小元素及其单测、归并排序及其单测、快速排序及其单测.

This commit is contained in:
huangzj
2020-08-20 09:28:35 +08:00
parent fa58e1a97f
commit c2aca17567
12 changed files with 385 additions and 8 deletions
+34
View File
@@ -0,0 +1,34 @@
/*
* @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
}