forked from yezihack/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
堆结构必须是完全二叉树,堆里每一个节点必须小于等于或大于等于其子树中的每个节点的值
- Loading branch information
Showing
8 changed files
with
271 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package _6_排序算法 | ||
|
||
//思路:申请最大值的数量的桶, 然后把数值存放在桶的下标里.再循环出>0的下标 | ||
|
||
func BucketSort(arr []int) { | ||
max := getMax(arr) | ||
bucket := make([]int, max + 1) | ||
for i := range arr { | ||
bucket[arr[i]] ++ | ||
} | ||
i := 0 | ||
for k, v := range bucket { | ||
for j := 0; j < v; j ++ { | ||
arr[i] = k | ||
i ++ | ||
} | ||
} | ||
} | ||
func getMax(arr []int) int { | ||
max := arr[0] | ||
for _, v := range arr { | ||
if v > max { | ||
max = v | ||
} | ||
} | ||
return max | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
package _9_查找算法 | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
) | ||
|
||
//二分查找: 每一次查找都是折半查找. | ||
//需要注意三点: left <= right | ||
// low + (hight-low)/2 或者 (low+high)>>1 | ||
// low=mid+1, high=mid-1 | ||
func BinarySearch(arr []int, value int) int { | ||
if len(arr) == 0 { | ||
return -1 | ||
} | ||
left, right := 0, len(arr) - 1 | ||
mid := 0 | ||
for left <= right { | ||
mid = (left + right) >> 1 | ||
if value == arr[mid] { | ||
return mid | ||
} else if arr[mid] > value { | ||
right = mid - 1 | ||
} else if arr[mid] < value { | ||
left = mid + 1 | ||
} | ||
} | ||
return -1 | ||
} | ||
//递归实现二分查找 | ||
func BReserveSearch(arr []int, low, high, value int) int { | ||
if low > high { | ||
return -1 | ||
} | ||
mid := (low + high) >> 1 | ||
if arr[mid] == value { | ||
return mid | ||
} else if arr[mid] > value { | ||
return BReserveSearch(arr, low, mid-1, value) | ||
} else { | ||
return BReserveSearch(arr, mid+1, high, value) | ||
} | ||
} | ||
//利用二分查找求平方根 | ||
func BSqrt(x float64) float64 { | ||
var ( | ||
low float64 = 0 | ||
high float64 = x | ||
mid float64 | ||
) | ||
if x > 0 { | ||
low = 1 | ||
high = x | ||
} else { | ||
low = x | ||
high = 1 | ||
} | ||
mid = low + (high - low) / 2 | ||
for float64(math.Abs(mid * mid - x)) > 0.000001 { | ||
if mid * mid < x { | ||
low = mid | ||
} else { | ||
high = mid | ||
} | ||
mid = low + (high - low) / 2 | ||
} | ||
return mid | ||
} | ||
//数组里出现多个重复的数字,获取最前面的数字下标,如1,2,2,3我们要找2,1后面的2而不是3前面的2 | ||
func BFirstSearch(arr []int, value int) int { | ||
low, high := 0, len(arr)-1 | ||
mid := 0 | ||
for low <= high { | ||
mid = low + (high - low) >> 1 | ||
if arr[mid] > value { | ||
high = mid - 1 | ||
} else if arr[mid] < value { | ||
low = mid + 1 | ||
} else { | ||
//如果是最左边的数,再向前找一个位置一定不是当前要找的值,否则high=mid-1继续向左找 | ||
if mid == 0 || arr[mid-1] != value { | ||
return mid | ||
} else { | ||
high = mid - 1 | ||
} | ||
} | ||
} | ||
return -1 | ||
} | ||
//数组里有多少重复的数,查找到最后一个数字.如1,2,2,3.我们要找到3前面的2,不是1后面的2 | ||
func BLastSearch(arr []int, value int) int { | ||
low, high := 0, len(arr) - 1 | ||
mid := 0 | ||
for low <= high { | ||
mid = low + (high - low) >> 1 | ||
if arr[mid] < value { | ||
low = mid + 1 | ||
} else if arr[mid] > value { | ||
high = mid - 1 | ||
} else { | ||
fmt.Println("mid", mid, "low", low, "high", high) | ||
if mid == len(arr) - 1 || arr[mid + 1] != value { | ||
return mid | ||
} else { | ||
low = mid + 1 | ||
} | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
//查找第一个大于等于给定值的元素 | ||
func BGtSearch(arr []int, value int) int { | ||
low, high := 0, len(arr)-1 | ||
mid := 0 | ||
for low <= high { | ||
mid = low + (high-low)>>1 | ||
if arr[mid] > value { | ||
high = mid - 1 | ||
} else if arr[mid] < value { | ||
low = mid + 1 | ||
} else { | ||
if mid == len(arr) - 1 || arr[mid + 1] > value { | ||
return mid + 1 | ||
} else { | ||
low = mid + 1 | ||
} | ||
} | ||
} | ||
return -1 | ||
} | ||
//查找最后一个小于等于给定值的元素 | ||
func BLTSearch(arr []int, value int) int { | ||
low, high := 0, len(arr) - 1 | ||
mid := 0 | ||
for low <= high { | ||
mid = low + (high - low) >> 1 | ||
if arr[mid] > value { | ||
high = mid - 1 | ||
} else if arr[mid] < value { | ||
low = mid + 1 | ||
} else { | ||
if mid == 0 || arr[mid-1] < value { | ||
return mid - 1 | ||
} else { | ||
high = mid -1 | ||
} | ||
} | ||
} | ||
return -1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package _9_查找算法 | ||
|
||
import ( | ||
"fmt" | ||
"github.com/yezihack/algo/00.src" | ||
"testing" | ||
) | ||
|
||
func TestBinarySearch(t *testing.T) { | ||
arr := []int{1,2,3,4,5,6,7} | ||
src.Asset(1, t, 3, BinarySearch(arr, 4)) | ||
src.Asset(2, t, 0, BinarySearch(arr, 1)) | ||
src.Asset(3, t, 6, BinarySearch(arr, 7)) | ||
src.Asset(4, t, -1, BinarySearch(arr, 70)) | ||
src.Asset(5, t, -1, BinarySearch(arr, -50)) | ||
} | ||
func TestBReserveSearch(t *testing.T) { | ||
arr := []int{1,2,3,4,5,6,7} | ||
src.Asset(1, t, 3, BReserveSearch(arr, 0, len(arr)-1, 4)) | ||
src.Asset(2, t, 0, BReserveSearch(arr, 0, len(arr)-1,1)) | ||
src.Asset(3, t, 6, BReserveSearch(arr, 0, len(arr)-1,7)) | ||
src.Asset(4, t, -1, BReserveSearch(arr, 0, len(arr)-1,70)) | ||
src.Asset(5, t, -1, BReserveSearch(arr, 0, len(arr)-1,-50)) | ||
} | ||
func TestBSqrt(t *testing.T) { | ||
var x float64 = 5 | ||
fmt.Println(BSqrt(x)) | ||
} | ||
func TestFirstSearch(t *testing.T) { | ||
arr := []int{1,2,3,3, 3,4,5} | ||
src.Asset(1, t, 2, BFirstSearch(arr, 3)) | ||
} | ||
func TestBLastSearch(t *testing.T) { | ||
arr := []int{1,2,3,3,4,5} | ||
src.Asset(1, t, 3, BLastSearch(arr, 3)) | ||
} | ||
func TestBGtSearch(t *testing.T) { | ||
arr := []int{1, 3, 5, 7, 9} | ||
src.Asset(1, t, 2, BGtSearch(arr, 4)) | ||
src.Asset(2, t, 1, BGtSearch(arr, 2)) | ||
src.Asset(3, t, 4, BGtSearch(arr, 9)) | ||
src.Asset(4, t, 0, BGtSearch(arr, 1)) | ||
src.Asset(5, t, 4, BGtSearch(arr, 10)) | ||
} | ||
func TestBLTSearch(t *testing.T) { | ||
arr := []int{1, 3, 5, 7, 9} | ||
src.Asset(1, t, 1, BLTSearch(arr, 4)) | ||
src.Asset(2, t, 0, BLTSearch(arr, 1)) | ||
src.Asset(3, t, 4, BLTSearch(arr, 9)) | ||
src.Asset(4, t, 3, BLTSearch(arr, 8)) | ||
src.Asset(5, t, 4, BLTSearch(arr, 81)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# 堆结构 | ||
> 一种特殊的树, 分小顶堆与大顶堆. | ||
1. 小顶堆的堆顶元素存储整个堆的最小元素.一般用于求TOP K的问题. | ||
1. 大顶堆的堆顶元素存储整个堆的最大元素,一般结合小顶堆求中位数.维护两个堆. | ||
|
||
## 堆的特性 | ||
1. 堆是一个特殊的树 | ||
1. 不稳定排序 | ||
1. 原地排序 | ||
1. 时间复杂度O(nlogn) | ||
1. 堆顶元素是最小值(或最大值),也可说 | ||
1. 属于完全二叉树 | ||
1. 一般使用数组存储. | ||
|
||
## 堆的操作 | ||
1. 堆化: 当插入新元素或删除堆顶元素,我们需要进行对堆的调整.让其重新满足堆的特性 | ||
1. 插入新元素:插入到最 | ||
1. 删除堆顶元素 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters