Comments (2)
作者你好,想问这里if的作用是什么?是否可以省略?因为去掉if不影响测试结果,代码如下:
package main
import "fmt"
func InsertSort(list []int) {
n := len(list)
// 进行 N-1 轮迭代
for i := 1; i <= n-1; i++ {
deal := list[i] // 待排序的数
j := i - 1 // 待排序的数左边的第一个数的位置
// 如果第一次比较,比左边的已排好序的第一个数小,那么进入处理
if deal < list[j] {//note:if判断作用???可以去掉吗???
// 一直往左边找,比待排序大的数都往后挪,腾空位给待排序插入
for ; j >= 0 && deal < list[j]; j-- {
list[j+1] = list[j] // 某数后移,给待排序留空位
}
list[j+1] = deal // 结束了,待排序的数插入空位
}
}
}
func InsertSortTest(s []int){
n := len(s)
for i:=1;i<=n-1;i++ {
insertNum:=s[i]//不能通过index交换,因为“list[j+1] = list[j]”可能导致原index-value对改变,所以只好利用数值而不是索引,注意与SelectSort区别
j:=i-1
for ; (j >= 0) && (insertNum< s[j]); j--{
s[j+1] = s[j]
}
s[j+1]=insertNum
}
}
func main() {
list := []int{5}
InsertSort(list)
fmt.Println(list)
s:=[]int{5}
InsertSortTest(s)
fmt.Println(s)
list1 := []int{5, 9}
InsertSort(list1)
fmt.Println(list1)
s1:=[]int{5, 9}
InsertSortTest(s1)
fmt.Println(s1)
list2 := []int{5, 9, 1, 6, 8, 14, 6, 49, 25, 4, 6, 3}
InsertSort(list2)
fmt.Println(list2)
s2:=[]int{5, 9, 1, 6, 8, 14, 6, 49, 25, 4, 6, 3}
InsertSortTest(s2)
fmt.Println(s2)
}
from goa.c.
作者你好,想问这里if的作用是什么?是否可以省略?因为去掉if不影响测试结果,代码如下:
package main import "fmt" func InsertSort(list []int) { n := len(list) // 进行 N-1 轮迭代 for i := 1; i <= n-1; i++ { deal := list[i] // 待排序的数 j := i - 1 // 待排序的数左边的第一个数的位置 // 如果第一次比较,比左边的已排好序的第一个数小,那么进入处理 if deal < list[j] {//note:if判断作用???可以去掉吗??? // 一直往左边找,比待排序大的数都往后挪,腾空位给待排序插入 for ; j >= 0 && deal < list[j]; j-- { list[j+1] = list[j] // 某数后移,给待排序留空位 } list[j+1] = deal // 结束了,待排序的数插入空位 } } } func InsertSortTest(s []int){ n := len(s) for i:=1;i<=n-1;i++ { insertNum:=s[i]//不能通过index交换,因为“list[j+1] = list[j]”可能导致原index-value对改变,所以只好利用数值而不是索引,注意与SelectSort区别 j:=i-1 for ; (j >= 0) && (insertNum< s[j]); j--{ s[j+1] = s[j] } s[j+1]=insertNum } } func main() { list := []int{5} InsertSort(list) fmt.Println(list) s:=[]int{5} InsertSortTest(s) fmt.Println(s) list1 := []int{5, 9} InsertSort(list1) fmt.Println(list1) s1:=[]int{5, 9} InsertSortTest(s1) fmt.Println(s1) list2 := []int{5, 9, 1, 6, 8, 14, 6, 49, 25, 4, 6, 3} InsertSort(list2) fmt.Println(list2) s2:=[]int{5, 9, 1, 6, 8, 14, 6, 49, 25, 4, 6, 3} InsertSortTest(s2) fmt.Println(s2) }
没错:
// 如果第一次比较,比左边的已排好序的第一个数小,那么进入处理
if deal < list[j] {
这个是可以去掉的,没有任何影响。下面的 for 循环会找到这个数的位置,然后插进去。
from goa.c.
Related Issues (20)
- 优先队列及堆排序
- 快速排序 HOT 5
- 查找算法
- 哈希表:散列查找
- 二叉查找树
- AVL树 HOT 1
- 2-3-4树和普通红黑树 HOT 3
- 文档部署 HOT 1
- 书籍推荐 HOT 1
- 参考
- 优先队列及堆排序目录问题 HOT 2
- 补充
- 堆合并:左偏树
- 优先队列及堆排序 HOT 3
- 纠正一处小错误 HOT 2
- 工程应用实例
- 勘误说明
- 博客title能不能加个图标,在浏览器书签有个图标好看一点点 HOT 1
- Official website address change HOT 1
- 简单入门golang-结构体和方法
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from goa.c.