Skip to content

Instantly share code, notes, and snippets.

@xmapst
Last active February 22, 2023 00:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xmapst/ede37b290bfe8171f0dff77638b21f21 to your computer and use it in GitHub Desktop.
Save xmapst/ede37b290bfe8171f0dff77638b21f21 to your computer and use it in GitHub Desktop.
冒泡排序的原理:对于一个数组里所有的元素进行两两比较,发生大于则变换数组下标则为升序排序,发生小于则变换数据下标的则为降序排序
package query
func Sort[T constraints.Ordered](buf []T) {
for i := 0; i < len(buf)-1; i++ {
flag := false
for j := 1; j < len(buf)-i; j++ {
if buf[j-1] > buf[j] {
tmp := buf[j-1]
buf[j-1] = buf[j]
buf[j] = tmp
flag = true
}
}
if !flag {
break
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment