Skip to content

Instantly share code, notes, and snippets.

@ivancorrales
Last active April 13, 2022 02:36
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 ivancorrales/5753e1011f5124edb83d21968f9a8f4c to your computer and use it in GitHub Desktop.
Save ivancorrales/5753e1011f5124edb83d21968f9a8f4c to your computer and use it in GitHub Desktop.
bubble sort implementation
package main
import "fmt"
func bubbleSort[T int32 | float32](input []T) []T {
swapped := true
for swapped {
swapped = false
for i := 0; i < len(input)-1; i++ {
if input[i] > input[i+1] {
input[i], input[i+1] = input[i+1], input[i]
swapped = true
}
}
}
return input
}
func main() {
list := []int32{8, 1, 4, 10, 7, 6}
sorted := bubbleSort(list)
fmt.Println(sorted)
listFloat := []float32{0.45, 1.12, 0.4, 1.20, 0.7, 2.6}
sortedFloat := bubbleSort(listFloat)
fmt.Println(sortedFloat)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment