Last active
April 13, 2022 02:36
-
-
Save ivancorrales/5753e1011f5124edb83d21968f9a8f4c to your computer and use it in GitHub Desktop.
bubble sort implementation
This file contains hidden or 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 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