冒泡排序的原理:对于一个数组里所有的元素进行两两比较,发生大于则变换数组下标则为升序排序,发生小于则变换数据下标的则为降序排序
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