Skip to content

Instantly share code, notes, and snippets.

@tacigar
Created July 25, 2021 04:58
Show Gist options
  • Save tacigar/f676a2b7c0f9935ed88f9eeee83159aa to your computer and use it in GitHub Desktop.
Save tacigar/f676a2b7c0f9935ed88f9eeee83159aa to your computer and use it in GitHub Desktop.
Go Generic Bubble Sort
package sort
type Ordered interface {
type int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, uintptr, float32, float64, string
}
func BubbleSort[T Ordered](slice []T) {
for i := 0; i < len(slice)-1; i++ {
done := true
for j := 1; j < len(slice)-i; j++ {
if slice[j] < slice[j-1] {
slice[j], slice[j-1] = slice[j-1], slice[j]
done = false
}
}
if done {
return
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment