Created
July 25, 2021 04:58
-
-
Save tacigar/f676a2b7c0f9935ed88f9eeee83159aa to your computer and use it in GitHub Desktop.
Go Generic Bubble Sort
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 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