diff --git a/golang_learn/data_func/go_to_sort/bubble_sort/main.go b/golang_learn/data_func/go_to_sort/bubble_sort/main.go deleted file mode 100644 index 1fb6221..0000000 --- a/golang_learn/data_func/go_to_sort/bubble_sort/main.go +++ /dev/null @@ -1,46 +0,0 @@ -package main - -// 冒泡排序 -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - Bubble(a) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Bubble 实现冒泡排序 O(n * n) - a : 被排序数组 - 返回:排序完毕数组 -*/ -func Bubble(a []int) { - for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 - for ii := i + 1; ii < j; ii++ { // 循环无序列表部分 - if a[i] > a[ii] { // 从一个元素与一个无序值比较 - a[ii], a[i] = a[i], a[ii] // 较大的值移至后边 - } - } - } -} - -/** -Bubble2 【优化】实现冒泡排序 O(n * n) - a : 被排序数组 - 返回:排序完毕数组 -*/ -func Bubble2(a []int) { - for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 - b := true // 定义变量优化 - for ii := i + 1; ii < j; ii++ { // 循环无序列表部分 - if a[i] > a[ii] { // 从一个元素与一个无序值比较 - a[ii], a[i] = a[i], a[ii] // 较大的值移至后边 - b = false // 发生交换 - } - } - if b { // 为存在交换,结束循环 - break - } - } -} diff --git a/golang_learn/data_func/go_to_sort/bucket_sort/main.go b/golang_learn/data_func/go_to_sort/bucket_sort/main.go deleted file mode 100644 index 357999e..0000000 --- a/golang_learn/data_func/go_to_sort/bucket_sort/main.go +++ /dev/null @@ -1,32 +0,0 @@ -package main - -// 桶排序 - 失败 -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - Bucket(a, len(a), 50) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Bucket 实现桶排序 -*/ -func Bucket(li []int, n, max int) { - // 创建桶 - a, b := make([][]int, n), max/n // a 桶 b 单桶的大小 - for i := range a { - a[i] = make([]int, 0) - } - for i := 0; i < n; i++ { - n := li[i] / b - a[n] = append(a[n], li[i]) - } - - for i, j := 0, n; i <= n; i = i + 1 { // 因为存在递归,将数值填回数组 - for ii, jj := 0, len(a[i]); ii < jj; ii, j = ii+1, j+1 { - li[j] = a[i][ii] - } - } -} diff --git a/golang_learn/data_func/go_to_sort/count_sort/main.go b/golang_learn/data_func/go_to_sort/count_sort/main.go deleted file mode 100644 index f05f907..0000000 --- a/golang_learn/data_func/go_to_sort/count_sort/main.go +++ /dev/null @@ -1,28 +0,0 @@ -package main - -// 计数排序 -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - Count(a, 50) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Count 实现计数排序 - 限制:仅限百内之整数 -*/ -func Count(li []int, max int) { - a := make([]int, max) - for i := 0; i < len(li); i++ { - a[li[i]] += 1 - } - - for i, j := 0, 0; i < max; i++ { - for ii := a[i]; ii > 0; ii, j = ii-1, j+1 { - li[j] = i - } - } -} diff --git a/golang_learn/data_func/go_to_sort/heap_sort/main.go b/golang_learn/data_func/go_to_sort/heap_sort/main.go deleted file mode 100644 index 1f2523b..0000000 --- a/golang_learn/data_func/go_to_sort/heap_sort/main.go +++ /dev/null @@ -1,57 +0,0 @@ -package main - -// 堆排序 -func main() { - - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - Heap(a) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Heap 进行排序 - 1. 建堆 - 2. 交换值并向下调整 -*/ -func Heap(li []int) { - n := len(li) - for i := (n - 2) / 2; i >= 0; i-- { // 创建堆 - sift(li, i, n-1) - } - - for i := n - 1; i >= 0; i-- { // 先做交换(减少内存)后做向下调整 - li[0], li[i] = li[i], li[0] - sift(li, 0, i-1) - } -} - -/** -sift 向下调整 - li 数组列表 - low 堆的根节点的下标 - high 堆的最后一个元素的下标 -*/ -func sift(li []int, low, high int) { - /** - i 指一层中的根 - j 开始是左孩子 - t 指一层中某个需要调整的值 - */ - i, j, t := low, 2*low+1, li[low] - for j <= high { // 在范围内进行循环,防止越界 - if j+1 <= high && li[j+1] > li[j] { // 比较一个根下的两个孩子大小,取大值的位置 - j = j + 1 - } - if li[j] > t { // 与指定根值比较,若值比根大,继续循环 - li[i], i, j = li[j], j, 2*i+1 - } else { - // li[i] = t // 将临时值放回,移至循环结束位置 - break - } - } - li[i] = t // 将临时值放回 - -} diff --git a/golang_learn/data_func/go_to_sort/insert_sort/main.go b/golang_learn/data_func/go_to_sort/insert_sort/main.go deleted file mode 100644 index 815e67b..0000000 --- a/golang_learn/data_func/go_to_sort/insert_sort/main.go +++ /dev/null @@ -1,33 +0,0 @@ -package main - -// 插入排序 -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - Insert(a) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Insert 实现插入排序 O(n * n) - a : 被排序数组 - 返回:排序完毕数组 -*/ -func Insert(s []int) { - n := len(s) - if n < 2 { - return - } - for i := 1; i < n; i++ { - for j := i; j > 0 && s[j] < s[j-1]; j-- { - // 选择第j张比较第j - 1张小,则循环交换较大的值 - swap(s, j, j-1) - } - } -} - -func swap(slice []int, i int, j int) { - slice[i], slice[j] = slice[j], slice[i] -} diff --git a/golang_learn/data_func/go_to_sort/merge_sort/main.go b/golang_learn/data_func/go_to_sort/merge_sort/main.go deleted file mode 100644 index 9eb875a..0000000 --- a/golang_learn/data_func/go_to_sort/merge_sort/main.go +++ /dev/null @@ -1,49 +0,0 @@ -package main - -// 归并排序 -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - MergeSort(a, 0, len(a)-1) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -MergeSort 实现归并排序(前提列表有序) - 包含:分解 + 合并 -*/ -func MergeSort(li []int, low, high int) { - if low < high { - mid := (low + high) / 2 - MergeSort(li, low, mid) - MergeSort(li, mid+1, high) - Merge(li, low, mid, high) - } -} - -/** -Merge 归并 -*/ -func Merge(li []int, low, mid, high int) { - i, j, lt, t := low, mid+1, make([]int, len(li)), 0 - for i <= mid && j <= high { // 将结果拼接到新的数组 - if li[i] < li[j] { - lt[t], i, t = li[i], i+1, t+1 - } else { - lt[t], j, t = li[j], j+1, t+1 - } - } - - for i <= mid { - lt[t], i, t = li[i], i+1, t+1 - } - for j <= high { - lt[t], j, t = li[j], j+1, t+1 - } - - for i, j := low, 0; i <= high; i, j = i+1, j+1 { // 因为存在递归,将数值填回数组 - li[i] = lt[j] - } -} diff --git a/golang_learn/data_func/go_to_sort/quick_sort/main.go b/golang_learn/data_func/go_to_sort/quick_sort/main.go deleted file mode 100644 index a94faf6..0000000 --- a/golang_learn/data_func/go_to_sort/quick_sort/main.go +++ /dev/null @@ -1,47 +0,0 @@ -package main - -// 快速排序 -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - Quick(a, 0, len(a)-1) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Quick 实现快速排序 O(n * log(n)) - a : 被排序数组 - 返回:排序完毕数组 -*/ -func Quick(slice []int, left, right int) { - if left < right { - mid := partition(slice, left, right) // 获取归位中值 - Quick(slice, left, mid-1) // 操作 中值 左边数组 - Quick(slice, mid+1, right) // 操作 中值 右边数组 - } -} - -/** -partition 归位函数,获取一个关于某值对称的数组 -*/ -func partition(slice []int, left, right int) int { - tmp := slice[left] // 获取左侧第一个数 - for left < right { - // 进行逐个实现归位操作,将列表形成一个对称列表 - // 左边比 tmp 大的数,调至右边 - for left < right && slice[right] >= tmp { - right-- - } - slice[left] = slice[right] - - // 右边比 tmp 小的数,调至左边 - for left < right && slice[left] <= tmp { - left++ - } - slice[right] = slice[left] - } - slice[left] = tmp - return left -} diff --git a/golang_learn/data_func/go_to_sort/select_sort/main.go b/golang_learn/data_func/go_to_sort/select_sort/main.go deleted file mode 100644 index cdd14c7..0000000 --- a/golang_learn/data_func/go_to_sort/select_sort/main.go +++ /dev/null @@ -1,30 +0,0 @@ -package main - -// 选择排序 -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - Select(a) - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Select 实现选择排序 O(n * n) - a : 被排序数组 - 返回:排序完毕数组 -*/ -func Select(a []int) { - for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 - theMin := i // 记录最小值记录 - for ii := i + 1; ii < j; ii++ { // 循环无序列表部分 - if a[theMin] > a[ii] { // 较大的值移至后边 - theMin = ii // 较大的值移至后边 - } - } - if theMin != i { - a[i], a[theMin] = a[theMin], a[i] - } - } -} diff --git a/golang_learn/data_func/go_to_sort/shell_sort/main.go b/golang_learn/data_func/go_to_sort/shell_sort/main.go deleted file mode 100644 index a17c681..0000000 --- a/golang_learn/data_func/go_to_sort/shell_sort/main.go +++ /dev/null @@ -1,38 +0,0 @@ -package main - -/*** -希尔排序 -*/ -func main() { - var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 - - for d := len(a); d >= 1; d = d / 2 { - Shell(a, d) - } - - for _, i2 := range a { - print(i2, "\t") // 输出每一个值 - } -} - -/** -Shell 实现希尔排序 O(n * n) - a : 被排序数组 - 返回:排序完毕数组 -*/ -func Shell(s []int, get int) { - n := len(s) - if n < 2 { - return - } - for i := get; i < n; i++ { - for j := i; j > 0 && s[j] < s[j-get]; j = j - get { - // 选择第j张比较第j - get张小,则循环交换较大的值 - swap(s, j, j-get) - } - } -} - -func swap(slice []int, i int, j int) { - slice[i], slice[j] = slice[j], slice[i] -} diff --git a/golang_learn/data_likou/数据流的中位数.go b/golang_learn/data_likou/数据流的中位数.go index 40c461f..d2628da 100644 --- a/golang_learn/data_likou/数据流的中位数.go +++ b/golang_learn/data_likou/数据流的中位数.go @@ -1,5 +1,10 @@ package main +import ( + "fmt" + "sort" +) + /*** 295. 数据流的中位数 中位数是有序列表中间的数。如果列表长度是偶数,中位数则是中间两个数的平均值。 @@ -20,39 +25,73 @@ package main 进阶: 如果数据流中所有整数都在 0 到 100 范围内,你将如何优化你的算法? 如果数据流中 99% 的整数都在 0 到 100 范围内,你将如何优化你的算法? - */ +*/ func main() { - + finder := Constructor() + finder.AddNum(1) + finder.AddNum(2) + fmt.Println(finder.FindMedian()) + finder.AddNum(3) + fmt.Println(finder.FindMedian()) } type MedianFinder struct { Nums []int } - /** initialize your data structure here. */ func Constructor() MedianFinder { - + return MedianFinder{[]int{}} } - -func (this *MedianFinder) AddNum(num int) { - for i, j := 0, len(this.Nums); i < j; i++ { - if this.Nums[i] > num { - this.Nums[] - } +func (this *MedianFinder) AddNum(num int) { + index := BinaryQuery(this.Nums, num) + if index == -1 { + var i []int + i = append(i, num) + this.Nums = append(i, this.Nums...) + } else { + a := append(this.Nums[:index], num) + this.Nums = append(a, this.Nums[index:]...) } } - func (this *MedianFinder) FindMedian() float64 { - + i := len(this.Nums) + ii := i / 2 + if i%2 == 0 { + return float64(this.Nums[ii]) + } + return float64(this.Nums[ii]+this.Nums[ii-1]) / 2 } - /** * Your MedianFinder object will be instantiated and called as such: * obj := Constructor(); * obj.AddNum(num); * param_2 := obj.FindMedian(); - */ \ No newline at end of file + */ + +// BinaryQuery 二分查找的实现,如果查找成功返回索引;否则返回-1 +func BinaryQuery(list []int, target int) int { + + // 定义并初始化low和high,声明mid + low, high := 0, len(list)-1 + + // 前提必须进行排序 + sort.Ints(list) + + for low <= high { + mid := (low + high) / 2 + + if list[mid] == target { + return mid + } else if list[mid] > target { + high = mid - 1 + } else { + low = mid + 1 + } + } + + return -1 +} diff --git a/golang_learn/data_paixu/02算法运行效率与时间复杂度.go b/golang_learn/data_paixu/02算法运行效率与时间复杂度.go index d33bae3..3325bfb 100644 --- a/golang_learn/data_paixu/02算法运行效率与时间复杂度.go +++ b/golang_learn/data_paixu/02算法运行效率与时间复杂度.go @@ -1,34 +1,44 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 0 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/06/29 15:29 228 05 ݹ.md -2021/06/29 15:29 581 06 ټ.md -2021/06/29 15:29 145 07 ˳.md -2021/06/29 15:29 212 08 ֲ.md -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 24 ļ 24,336 ֽ - 3 Ŀ¼ 179,901,222,912 ֽ +package main + +import ( + "fmt" + "strconv" +) + +func main() { + + // 时间复杂度=o(1) + fmt.Println("1 Hello World!") + + // 时间复杂度=o(n) + for i, n := 0, 100; i < n; i++ { + fmt.Println(strconv.Itoa(i), " Hello World!") + } + + // 时间复杂度=o(n) + for i, n, m := 0, 100, 10; i < n; i++ { + for j := 0; j < m; j++ { + fmt.Println(strconv.Itoa(i), strconv.Itoa(j), " Hello World!") + } + } + + // 时间复杂度=o(1) + fmt.Println("1 Hello World!") + fmt.Println("2 Hello World!") + fmt.Println("3 Hello World!") + + // 时间复杂度=o(n2) + for i, n, m := 0, 100, 10; i < n; i++ { + fmt.Println(strconv.Itoa(i), " Hello World!") + for j := 0; j < m; j++ { + fmt.Println(strconv.Itoa(j), " Hello World!") + } + } + + // 时间复杂度=o(log n) + n := 100 + for n > 1 { + fmt.Println(strconv.Itoa(n), " Hello World!") + n /= 2 + } +} diff --git a/golang_learn/data_paixu/03简单判断时间复杂度.go b/golang_learn/data_paixu/03简单判断时间复杂度.go deleted file mode 100644 index f926d85..0000000 --- a/golang_learn/data_paixu/03简单判断时间复杂度.go +++ /dev/null @@ -1,35 +0,0 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 0 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/06/29 15:29 228 05 ݹ.md -2021/06/29 15:29 581 06 ټ.md -2021/06/29 15:29 145 07 ˳.md -2021/06/29 15:29 212 08 ֲ.md -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 25 ļ 25,945 ֽ - 3 Ŀ¼ 179,901,218,816 ֽ diff --git a/golang_learn/data_paixu/04空间复杂度.go b/golang_learn/data_paixu/04空间复杂度.go deleted file mode 100644 index f34ca53..0000000 --- a/golang_learn/data_paixu/04空间复杂度.go +++ /dev/null @@ -1,36 +0,0 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 0 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/06/29 15:29 581 06 ټ.md -2021/06/29 15:29 145 07 ˳.md -2021/06/29 15:29 212 08 ֲ.md -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 26 ļ 27,615 ֽ - 3 Ŀ¼ 179,901,214,720 ֽ diff --git a/golang_learn/data_paixu/05递归.go b/golang_learn/data_paixu/05递归.go deleted file mode 100644 index 27b2188..0000000 --- a/golang_learn/data_paixu/05递归.go +++ /dev/null @@ -1,37 +0,0 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 0 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/06/29 15:29 145 07 ˳.md -2021/06/29 15:29 212 08 ֲ.md -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 27 ļ 29,338 ֽ - 3 Ŀ¼ 179,901,210,624 ֽ diff --git a/golang_learn/data_paixu/06汉偌塔问题.go b/golang_learn/data_paixu/06汉偌塔问题.go index 0139527..c4d006f 100644 --- a/golang_learn/data_paixu/06汉偌塔问题.go +++ b/golang_learn/data_paixu/06汉偌塔问题.go @@ -1,38 +1,21 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +import ( + "fmt" +) -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 0 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/06/29 15:29 212 08 ֲ.md -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 28 ļ 31,108 ֽ - 3 Ŀ¼ 179,901,206,528 ֽ +/*** +问题:将 n 格元素从 A 经过 B 移动到 C +小数 必须在 大数的上侧,及存在顺序问题 +*/ +func main() { + run(3, "A", "B", "C") +} + +func run(n int, a, b, c string) { + if n > 0 { + run(n-1, a, c, b) + fmt.Println("从【" + a + "】移动到【" + c + "】") + run(n-1, b, a, c) + } +} diff --git a/golang_learn/data_paixu/06汉偌塔问题.md b/golang_learn/data_paixu/06汉偌塔问题.md index b1297ed..52f9b35 100644 --- a/golang_learn/data_paixu/06汉偌塔问题.md +++ b/golang_learn/data_paixu/06汉偌塔问题.md @@ -11,23 +11,24 @@ * 代码 -```java - public static void main(String[] args) { - run(3, "A", "B", "C"); - } - - private static void run(Integer n, String a, String b, String c) { - if (n>0) { - run(n-1, a, c, b); - System.out.println("从【"+a+"】移动到【"+c+"】"); - run(n-1, b, a, c); - } +```go +func main() { + run(3, "A", "B", "C") +} + +func run(n int, a, b, c string) { + if n > 0 { + run(n-1, a, c, b) + fmt.Println("从【"+a+"】移动到【"+c+"】") + run(n-1, b, a, c) } +} ``` -```go -``` +## 总结 +对于潜在存在递归操作的操作,思考问题的最初状态。 +简化使用 n = 1时应该进行的操作。依次推导 n = 2, n = 3 ... 得出结论。 diff --git a/golang_learn/data_paixu/07顺序查找.go b/golang_learn/data_paixu/07顺序查找.go index 265e22e..d540561 100644 --- a/golang_learn/data_paixu/07顺序查找.go +++ b/golang_learn/data_paixu/07顺序查找.go @@ -1,39 +1,18 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +import "fmt" -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 0 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 29 ļ 32,931 ֽ - 3 Ŀ¼ 179,901,202,432 ֽ +func main() { + + list := []int{1, 2, 3, 45, 13, 16, 7} + fmt.Println(ListQuery(list, 13)) +} + +func ListQuery(list []int, k int) int { + for i, j := 0, len(list); i < j; i++ { + if list[i] == k { + return i + } + } + return -1 +} diff --git a/golang_learn/data_paixu/07顺序查找.md b/golang_learn/data_paixu/07顺序查找.md index 7ac7ecb..0a850da 100644 --- a/golang_learn/data_paixu/07顺序查找.md +++ b/golang_learn/data_paixu/07顺序查找.md @@ -4,11 +4,19 @@ * **列表查找**(线性查找):从列表中查找指定元素 * **代码** -```java - -``` - ```go +func main() { + + fmt.Println(listQuery(13)) +} +func listQuery(k int) int { + for i := 0; i < 20; i++ { + if i == k { + return i + } + } + return -1 +} ``` diff --git a/golang_learn/data_paixu/08二分查找.go b/golang_learn/data_paixu/08二分查找.go index 0a41ccc..2327f00 100644 --- a/golang_learn/data_paixu/08二分查找.go +++ b/golang_learn/data_paixu/08二分查找.go @@ -1,40 +1,38 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 0 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 30 ļ 34,805 ֽ - 3 Ŀ¼ 179,901,198,336 ֽ +package main + +import ( + "fmt" + "sort" +) + +func main() { + + list := []int{1, 2, 3, 45, 13, 16, 7} + fmt.Println(BinaryQuery(list, 13)) +} + +// BinaryQuery 二分查找的实现,如果查找成功返回索引;否则返回-1 +func BinaryQuery(list []int, target int) int { + + // 定义并初始化low和high,声明mid + low, high := 0, len(list)-1 + + // 前提必须进行排序 + sort.Ints(list) + fmt.Println(list) + + for low <= high { + mid := (low + high) / 2 + fmt.Println(low, high, mid) + + if list[mid] == target { + return mid + } else if list[mid] > target { + high = mid - 1 + } else { + low = mid + 1 + } + } + + return -1 +} diff --git a/golang_learn/data_paixu/08二分查找.md b/golang_learn/data_paixu/08二分查找.md index cdc8c7c..1a99341 100644 --- a/golang_learn/data_paixu/08二分查找.md +++ b/golang_learn/data_paixu/08二分查找.md @@ -6,13 +6,37 @@ * **候选区**(左<右) * 左>右,不存在后选取区 * **原理** + * **前提** 必须进行排序 * **代码** -```java +```go +func main() { + list := []int{1, 2, 3, 45, 13, 16, 7} + fmt.Println(BinaryQuery(list, 13)) +} -``` +// BinaryQuery 二分查找的实现,如果查找成功返回索引;否则返回-1 +func BinaryQuery(list []int, target int) int { -```go + // 定义并初始化low和high,声明mid + low, high := 0, len(list)-1 + + // 前提必须进行排序 + sort.Ints(list) + + for low <= high { + mid := (low + high) / 2 + + if list[mid] == target { + return mid + }else if list[mid] > target { + high = mid - 1 + }else { + low = mid + 1; + } + } + return -1 +} ``` diff --git a/golang_learn/data_paixu/09查找方式对比.go b/golang_learn/data_paixu/09查找方式对比.go deleted file mode 100644 index 122b59c..0000000 --- a/golang_learn/data_paixu/09查找方式对比.go +++ /dev/null @@ -1,41 +0,0 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 0 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 31 ļ 36,730 ֽ - 3 Ŀ¼ 179,901,194,240 ֽ diff --git a/golang_learn/data_paixu/09查找方式对比.md b/golang_learn/data_paixu/09查找方式对比.md index e92dc15..b531078 100644 --- a/golang_learn/data_paixu/09查找方式对比.md +++ b/golang_learn/data_paixu/09查找方式对比.md @@ -6,6 +6,3 @@ * **线性查找** * 复杂度O(n) - - -* \ No newline at end of file diff --git a/golang_learn/data_paixu/10排序.go b/golang_learn/data_paixu/10排序.go deleted file mode 100644 index 29d3ce7..0000000 --- a/golang_learn/data_paixu/10排序.go +++ /dev/null @@ -1,42 +0,0 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 0 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 32 ļ 38,710 ֽ - 3 Ŀ¼ 179,901,190,144 ֽ diff --git a/golang_learn/data_paixu/11冒泡排序.go b/golang_learn/data_paixu/11冒泡排序.go index 9f74e4b..2ea97bb 100644 --- a/golang_learn/data_paixu/11冒泡排序.go +++ b/golang_learn/data_paixu/11冒泡排序.go @@ -1,43 +1,93 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +import "fmt" -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 0 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 33 ļ 40,737 ֽ - 3 Ŀ¼ 179,901,186,048 ֽ +func main() { + + fmt.Println("执行 Bubble") + fmt.Println(Bubble([]int{2, 1, 45, 13, 16, 3, 7})) + fmt.Println("执行 Bubble2") + fmt.Println(Bubble2([]int{2, 1, 45, 13, 16, 3, 7})) + fmt.Println("执行 Bubble3") + fmt.Println(Bubble3([]int{2, 1, 45, 13, 16, 3, 7})) + fmt.Println("执行 Bubble4——1") + fmt.Println(Bubble4([]int{2, 1, 45, 13, 16, 3, 7})) + fmt.Println("执行 Bubble4——2") + fmt.Println(Bubble4([]int{1, 2, 3, 4, 5, 6, 7})) + fmt.Println("执行 Bubble4——3") + fmt.Println(Bubble4([]int{7, 6, 5, 4, 3, 2, 1})) +} + +/** +Bubble 【优化】实现冒泡排序 + a : 被排序数组 + 返回:排序完毕数组 +*/ +func Bubble(a []int) []int { + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + b := true // 定义变量优化 + for ii := 0; ii < j-1; ii++ { // 循环无序列表部分 + if a[i] < a[ii] { // 从一个元素与一个无序值比较 + a[ii], a[i] = a[i], a[ii] // 较大的值移至后边 + b = false // 发生交换 + } + } + if b { // 为存在交换,结束循环 + break + } + } + return a +} + +/** +Bubble2 实现冒泡排序 + a : 被排序数组 + 返回 :排序完毕数组 +*/ +func Bubble2(a []int) []int { + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + for ii := i + 1; ii < j; ii++ { // 循环无序列表部分 + if a[i] > a[ii] { // 从一个元素与一个无序值比较 + a[ii], a[i] = a[i], a[ii] // 较大的值移至后边 + } + } + } + return a +} + +/** +Bubble3 【异同】实现冒泡排序 + a : 被排序数组 + 返回 :排序完毕数组 +*/ +func Bubble3(a []int) []int { + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + for ii := i; ii < j-1; ii++ { // 循环无序列表部分 + if a[ii] > a[ii+1] { // 从一个元素与一个无序值比较 + a[ii], a[ii+1] = a[ii+1], a[ii] // 较大的值移至后边 + } + } + } + return a +} + +/** +Bubble4 【异同-优化】实现冒泡排序 + a : 被排序数组 + 返回 :排序完毕数组 +*/ +func Bubble4(a []int) []int { + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + b := true // 定义变量优化 + for ii := 0; ii < j-1; ii++ { // 循环无序列表部分 + if a[ii] > a[ii+1] { // 从一个元素与一个无序值比较 + a[ii], a[ii+1] = a[ii+1], a[ii] // 较大的值移至后边 + b = false + } + } + if b { // 为存在交换,结束循环 + break + } + } + return a +} diff --git a/golang_learn/data_paixu/11冒泡排序.md b/golang_learn/data_paixu/11冒泡排序.md index 45ee64a..e81659c 100644 --- a/golang_learn/data_paixu/11冒泡排序.md +++ b/golang_learn/data_paixu/11冒泡排序.md @@ -11,8 +11,22 @@ * 4.持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 * **代码** -```java - +```go +/** +Bubble 【异同】实现冒泡排序 + a : 被排序数组 + 返回 :排序完毕数组 +*/ +func Bubble(a []int) []int { + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + for ii := i; ii < j - 1; ii++ { // 循环无序列表部分 + if a[ii] > a[ii + 1] { // 从一个元素与一个无序值比较 + a[ii], a[ii + 1] = a[ii + 1], a[ii] // 较大的值移至后边 + } + } + } + return a +} ``` ```go @@ -22,8 +36,8 @@ Bubble 实现冒泡排序 返回:排序完毕数组 */ func Bubble(a []int) []int { - for i,j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 - for ii := i + 1; ii < j ; ii++ { // 循环无序列表部分 + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + for ii := i + 1; ii < j; ii++ { // 循环无序列表部分 if a[i] > a[ii] { // 从一个元素与一个无序值比较 a[ii], a[i] = a[i], a[ii] // 较大的值移至后边 } @@ -38,19 +52,19 @@ func Bubble(a []int) []int { Bubble 【优化】实现冒泡排序 a : 被排序数组 返回:排序完毕数组 - */ +*/ func Bubble(a []int) []int { - for i,j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 - b := true // 定义变量优化 - for ii := i + 1; ii < j ; ii++ { // 循环无序列表部分 - if a[i] > a[ii] { // 从一个元素与一个无序值比较 + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + b := true // 定义变量优化 + for ii := 0; ii < j - 1; ii++ { // 循环无序列表部分 + if a[i] < a[ii] { // 从一个元素与一个无序值比较 a[ii], a[i] = a[i], a[ii] // 较大的值移至后边 - b = false // 发生交换 + b = false // 发生交换 } } - if b { // 为存在交换,结束循环 - break - } + if b { // 为存在交换,结束循环 + break + } } return a } diff --git a/golang_learn/data_paixu/12选择排序.go b/golang_learn/data_paixu/12选择排序.go index 6515173..cdd14c7 100644 --- a/golang_learn/data_paixu/12选择排序.go +++ b/golang_learn/data_paixu/12选择排序.go @@ -1,44 +1,30 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +// 选择排序 +func main() { + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 + Select(a) -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 0 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 34 ļ 42,815 ֽ - 3 Ŀ¼ 179,901,181,952 ֽ + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +Select 实现选择排序 O(n * n) + a : 被排序数组 + 返回:排序完毕数组 +*/ +func Select(a []int) { + for i, j := 0, len(a); i < j; i++ { // 循环列表元素每一个元素 + theMin := i // 记录最小值记录 + for ii := i + 1; ii < j; ii++ { // 循环无序列表部分 + if a[theMin] > a[ii] { // 较大的值移至后边 + theMin = ii // 较大的值移至后边 + } + } + if theMin != i { + a[i], a[theMin] = a[theMin], a[i] + } + } +} diff --git a/golang_learn/data_paixu/13插入排序.go b/golang_learn/data_paixu/13插入排序.go index 372d603..107c060 100644 --- a/golang_learn/data_paixu/13插入排序.go +++ b/golang_learn/data_paixu/13插入排序.go @@ -1,45 +1,33 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +// 插入排序 +func main() { + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 + Insert(a) -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 0 13.go -2021/06/29 15:29 2,018 14 .md -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 35 ļ 44,944 ֽ - 3 Ŀ¼ 179,901,177,856 ֽ + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +Insert 实现插入排序 O(n * n) + a : 被排序数组 + 返回:排序完毕数组 +*/ +func Insert(s []int) { + n := len(s) + if n < 2 { + return + } + for i := 1; i < n; i++ { + for j := i; j > 0 && s[j] < s[j-1]; j-- { + // 选择第j张比较第j - 1张小,则循环交换较大的值 + swap2(s, j, j-1) + } + } +} + +func swap2(slice []int, i int, j int) { + slice[i], slice[j] = slice[j], slice[i] +} diff --git a/golang_learn/data_paixu/14快速排序.go b/golang_learn/data_paixu/14快速排序.go index cb6a225..a94faf6 100644 --- a/golang_learn/data_paixu/14快速排序.go +++ b/golang_learn/data_paixu/14快速排序.go @@ -1,46 +1,47 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +// 快速排序 +func main() { + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 + Quick(a, 0, len(a)-1) -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 0 14.go -2021/06/29 15:29 1,345 15 .md -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 36 ļ 47,124 ֽ - 3 Ŀ¼ 179,901,173,760 ֽ + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +Quick 实现快速排序 O(n * log(n)) + a : 被排序数组 + 返回:排序完毕数组 +*/ +func Quick(slice []int, left, right int) { + if left < right { + mid := partition(slice, left, right) // 获取归位中值 + Quick(slice, left, mid-1) // 操作 中值 左边数组 + Quick(slice, mid+1, right) // 操作 中值 右边数组 + } +} + +/** +partition 归位函数,获取一个关于某值对称的数组 +*/ +func partition(slice []int, left, right int) int { + tmp := slice[left] // 获取左侧第一个数 + for left < right { + // 进行逐个实现归位操作,将列表形成一个对称列表 + // 左边比 tmp 大的数,调至右边 + for left < right && slice[right] >= tmp { + right-- + } + slice[left] = slice[right] + + // 右边比 tmp 小的数,调至左边 + for left < right && slice[left] <= tmp { + left++ + } + slice[right] = slice[left] + } + slice[left] = tmp + return left +} diff --git a/golang_learn/data_paixu/15树.go b/golang_learn/data_paixu/15树.go deleted file mode 100644 index b4de9ab..0000000 --- a/golang_learn/data_paixu/15树.go +++ /dev/null @@ -1,47 +0,0 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 2,231 14.go -2021/06/29 15:29 1,345 15 .md -2021/08/27 17:10 0 15.go -2021/06/29 15:29 2,609 16 .md -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 37 ļ 49,355 ֽ - 3 Ŀ¼ 179,901,169,664 ֽ diff --git a/golang_learn/data_paixu/16堆排序.go b/golang_learn/data_paixu/16堆排序.go index 751f48b..1f2523b 100644 --- a/golang_learn/data_paixu/16堆排序.go +++ b/golang_learn/data_paixu/16堆排序.go @@ -1,48 +1,57 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +// 堆排序 +func main() { -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 2,231 14.go -2021/06/29 15:29 1,345 15 .md -2021/08/27 17:10 2,276 15.go -2021/06/29 15:29 2,609 16 .md -2021/08/27 17:10 0 16.go -2021/06/29 15:29 1,646 17 鲢.md -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 38 ļ 51,631 ֽ - 3 Ŀ¼ 179,901,165,568 ֽ + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 + Heap(a) + + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +Heap 进行排序 + 1. 建堆 + 2. 交换值并向下调整 +*/ +func Heap(li []int) { + n := len(li) + for i := (n - 2) / 2; i >= 0; i-- { // 创建堆 + sift(li, i, n-1) + } + + for i := n - 1; i >= 0; i-- { // 先做交换(减少内存)后做向下调整 + li[0], li[i] = li[i], li[0] + sift(li, 0, i-1) + } +} + +/** +sift 向下调整 + li 数组列表 + low 堆的根节点的下标 + high 堆的最后一个元素的下标 +*/ +func sift(li []int, low, high int) { + /** + i 指一层中的根 + j 开始是左孩子 + t 指一层中某个需要调整的值 + */ + i, j, t := low, 2*low+1, li[low] + for j <= high { // 在范围内进行循环,防止越界 + if j+1 <= high && li[j+1] > li[j] { // 比较一个根下的两个孩子大小,取大值的位置 + j = j + 1 + } + if li[j] > t { // 与指定根值比较,若值比根大,继续循环 + li[i], i, j = li[j], j, 2*i+1 + } else { + // li[i] = t // 将临时值放回,移至循环结束位置 + break + } + } + li[i] = t // 将临时值放回 + +} diff --git a/golang_learn/data_paixu/17归并排序.go b/golang_learn/data_paixu/17归并排序.go index cdfaa51..9eb875a 100644 --- a/golang_learn/data_paixu/17归并排序.go +++ b/golang_learn/data_paixu/17归并排序.go @@ -1,49 +1,49 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +// 归并排序 +func main() { + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 + MergeSort(a, 0, len(a)-1) -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 2,231 14.go -2021/06/29 15:29 1,345 15 .md -2021/08/27 17:10 2,276 15.go -2021/06/29 15:29 2,609 16 .md -2021/08/27 17:10 2,325 16.go -2021/06/29 15:29 1,646 17 鲢.md -2021/08/27 17:10 0 17鲢.go -2021/06/29 15:29 1,284 18 ϣ.md -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 39 ļ 53,956 ֽ - 3 Ŀ¼ 179,901,161,472 ֽ + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +MergeSort 实现归并排序(前提列表有序) + 包含:分解 + 合并 +*/ +func MergeSort(li []int, low, high int) { + if low < high { + mid := (low + high) / 2 + MergeSort(li, low, mid) + MergeSort(li, mid+1, high) + Merge(li, low, mid, high) + } +} + +/** +Merge 归并 +*/ +func Merge(li []int, low, mid, high int) { + i, j, lt, t := low, mid+1, make([]int, len(li)), 0 + for i <= mid && j <= high { // 将结果拼接到新的数组 + if li[i] < li[j] { + lt[t], i, t = li[i], i+1, t+1 + } else { + lt[t], j, t = li[j], j+1, t+1 + } + } + + for i <= mid { + lt[t], i, t = li[i], i+1, t+1 + } + for j <= high { + lt[t], j, t = li[j], j+1, t+1 + } + + for i, j := low, 0; i <= high; i, j = i+1, j+1 { // 因为存在递归,将数值填回数组 + li[i] = lt[j] + } +} diff --git a/golang_learn/data_paixu/18希尔排序.go b/golang_learn/data_paixu/18希尔排序.go index d472526..f6d33a1 100644 --- a/golang_learn/data_paixu/18希尔排序.go +++ b/golang_learn/data_paixu/18希尔排序.go @@ -1,50 +1,38 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +/*** +希尔排序 +*/ +func main() { + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 2,231 14.go -2021/06/29 15:29 1,345 15 .md -2021/08/27 17:10 2,276 15.go -2021/06/29 15:29 2,609 16 .md -2021/08/27 17:10 2,325 16.go -2021/06/29 15:29 1,646 17 鲢.md -2021/08/27 17:10 2,376 17鲢.go -2021/06/29 15:29 1,284 18 ϣ.md -2021/08/27 17:10 0 18ϣ.go -2021/06/29 15:29 1,442 19 .md -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 40 ļ 56,332 ֽ - 3 Ŀ¼ 179,901,153,280 ֽ + for d := len(a); d >= 1; d = d / 2 { + Shell(a, d) + } + + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +Shell 实现希尔排序 O(n * n) + a : 被排序数组 + 返回:排序完毕数组 +*/ +func Shell(s []int, get int) { + n := len(s) + if n < 2 { + return + } + for i := get; i < n; i++ { + for j := i; j > 0 && s[j] < s[j-get]; j = j - get { + // 选择第j张比较第j - get张小,则循环交换较大的值 + swap1(s, j, j-get) + } + } +} + +func swap1(slice []int, i int, j int) { + slice[i], slice[j] = slice[j], slice[i] +} diff --git a/golang_learn/data_paixu/19计数排序.go b/golang_learn/data_paixu/19计数排序.go index 4415e76..f05f907 100644 --- a/golang_learn/data_paixu/19计数排序.go +++ b/golang_learn/data_paixu/19计数排序.go @@ -1,51 +1,28 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +// 计数排序 +func main() { + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 + Count(a, 50) -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 2,231 14.go -2021/06/29 15:29 1,345 15 .md -2021/08/27 17:10 2,276 15.go -2021/06/29 15:29 2,609 16 .md -2021/08/27 17:10 2,325 16.go -2021/06/29 15:29 1,646 17 鲢.md -2021/08/27 17:10 2,376 17鲢.go -2021/06/29 15:29 1,284 18 ϣ.md -2021/08/27 17:10 2,427 18ϣ.go -2021/06/29 15:29 1,442 19 .md -2021/08/27 17:10 0 19.go -2021/06/29 15:29 362 20 Ͱ.md -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 41 ļ 58,759 ֽ - 3 Ŀ¼ 179,901,149,184 ֽ + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +Count 实现计数排序 + 限制:仅限百内之整数 +*/ +func Count(li []int, max int) { + a := make([]int, max) + for i := 0; i < len(li); i++ { + a[li[i]] += 1 + } + + for i, j := 0, 0; i < max; i++ { + for ii := a[i]; ii > 0; ii, j = ii-1, j+1 { + li[j] = i + } + } +} diff --git a/golang_learn/data_paixu/20桶排序.go b/golang_learn/data_paixu/20桶排序.go index fbd17f0..357999e 100644 --- a/golang_learn/data_paixu/20桶排序.go +++ b/golang_learn/data_paixu/20桶排序.go @@ -1,52 +1,32 @@ - D еľ Data - к D649-8FB3 +package main - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ +// 桶排序 - 失败 +func main() { + var a = []int{29, 10, 14, 37, 25, 18, 30} // 创建数组的简写声明 + Bucket(a, len(a), 50) -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 2,231 14.go -2021/06/29 15:29 1,345 15 .md -2021/08/27 17:10 2,276 15.go -2021/06/29 15:29 2,609 16 .md -2021/08/27 17:10 2,325 16.go -2021/06/29 15:29 1,646 17 鲢.md -2021/08/27 17:10 2,376 17鲢.go -2021/06/29 15:29 1,284 18 ϣ.md -2021/08/27 17:10 2,427 18ϣ.go -2021/06/29 15:29 1,442 19 .md -2021/08/27 17:10 2,478 19.go -2021/06/29 15:29 362 20 Ͱ.md -2021/08/27 17:10 0 20Ͱ.go -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 42 ļ 61,237 ֽ - 3 Ŀ¼ 179,901,145,088 ֽ + for _, i2 := range a { + print(i2, "\t") // 输出每一个值 + } +} + +/** +Bucket 实现桶排序 +*/ +func Bucket(li []int, n, max int) { + // 创建桶 + a, b := make([][]int, n), max/n // a 桶 b 单桶的大小 + for i := range a { + a[i] = make([]int, 0) + } + for i := 0; i < n; i++ { + n := li[i] / b + a[n] = append(a[n], li[i]) + } + + for i, j := 0, n; i <= n; i = i + 1 { // 因为存在递归,将数值填回数组 + for ii, jj := 0, len(a[i]); ii < jj; ii, j = ii+1, j+1 { + li[j] = a[i][ii] + } + } +} diff --git a/golang_learn/data_paixu/21排序方式对比.go b/golang_learn/data_paixu/21排序方式对比.go deleted file mode 100644 index 837d08d..0000000 --- a/golang_learn/data_paixu/21排序方式对比.go +++ /dev/null @@ -1,53 +0,0 @@ - D еľ Data - к D649-8FB3 - - D:\ViviCode\golangs_learn\golang_learn\data_paixu Ŀ¼ - -2021/08/27 17:10 . -2021/08/27 17:10 .. -2021/06/29 15:29 124 01 㷨Ÿ.md -2021/08/27 17:10 1,542 01㷨Ÿ.go -2021/06/29 15:29 1,779 02 㷨Чʱ临Ӷ.md -2021/08/27 17:10 1,609 02㷨Чʱ临Ӷ.go -2021/06/29 15:29 406 03 жʱ临Ӷ.md -2021/08/27 17:10 1,670 03жʱ临Ӷ.go -2021/06/29 15:29 321 04 ռ临Ӷ.md -2021/08/27 17:10 1,723 04ռ临Ӷ.go -2021/06/29 15:29 228 05 ݹ.md -2021/08/27 17:10 1,770 05ݹ.go -2021/06/29 15:29 581 06 ټ.md -2021/08/27 17:10 1,823 06ټ.go -2021/06/29 15:29 145 07 ˳.md -2021/08/27 17:10 1,874 07˳.go -2021/06/29 15:29 212 08 ֲ.md -2021/08/27 17:10 1,925 08ֲ.go -2021/06/29 15:29 163 09 ҷʽԱ.md -2021/08/27 17:10 1,980 09ҷʽԱ.go -2021/06/29 15:29 484 10 .md -2021/08/27 17:10 2,027 10.go -2021/06/29 15:29 2,351 11 ð.md -2021/08/27 17:10 2,078 11ð.go -2021/06/29 15:29 2,161 12 ѡ.md -2021/08/27 17:10 2,129 12ѡ.go -2021/06/29 15:29 1,588 13 .md -2021/08/27 17:10 2,180 13.go -2021/06/29 15:29 2,018 14 .md -2021/08/27 17:10 2,231 14.go -2021/06/29 15:29 1,345 15 .md -2021/08/27 17:10 2,276 15.go -2021/06/29 15:29 2,609 16 .md -2021/08/27 17:10 2,325 16.go -2021/06/29 15:29 1,646 17 鲢.md -2021/08/27 17:10 2,376 17鲢.go -2021/06/29 15:29 1,284 18 ϣ.md -2021/08/27 17:10 2,427 18ϣ.go -2021/06/29 15:29 1,442 19 .md -2021/08/27 17:10 2,478 19.go -2021/06/29 15:29 362 20 Ͱ.md -2021/08/27 17:10 2,527 20Ͱ.go -2021/06/29 15:29 1,103 21 ʽԱ.md -2021/08/27 17:10 0 21ʽԱ.go -2021/08/27 17:10 442 a.txt -2021/08/27 17:05 img - 43 ļ 63,764 ֽ - 3 Ŀ¼ 179,901,140,992 ֽ