一个已经有序的数据序列,要求在这个已经排好的数据序列中插入一个数,但要求插入后此数据序列仍然有序,这个时候就要用到一种新的排序方法——插入排序法
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
func Sort[T constraints.Ordered](buf []T) { | |
for i := 1; i < len(buf); i++ { | |
for j := i; j > 0; j-- { | |
if buf[j] < buf[j-1] { | |
tmp := buf[j-1] | |
buf[j-1] = buf[j] | |
buf[j] = tmp | |
} else { | |
break | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment