Last active
February 22, 2023 00:51
-
-
Save xmapst/ede37b290bfe8171f0dff77638b21f21 to your computer and use it in GitHub Desktop.
冒泡排序的原理:对于一个数组里所有的元素进行两两比较,发生大于则变换数组下标则为升序排序,发生小于则变换数据下标的则为降序排序
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
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