Skip to content

Instantly share code, notes, and snippets.

@literadix
Created June 4, 2019 20:48
Show Gist options
  • Save literadix/1dae922138eaec8227638ee69fa7145e to your computer and use it in GitHub Desktop.
Save literadix/1dae922138eaec8227638ee69fa7145e to your computer and use it in GitHub Desktop.
package main
import "fmt"
// https://www.coursera.org/learn/golang-functions-methods/peer/UxoIW/module-1-activity-bubble-sort-program/submit
// https://tutorialedge.net/golang/implementing-bubble-sort-with-golang/
func BubbleSort(input []int) {
n := len(input)
swapped := true
for swapped {
swapped = false
for i := 1; i < n; i++ {
if input[i-1] > input[i] {
input[i], input[i-1] = input[i-1], input[i]
swapped = true
}
}
}
}
func main() {
values := []int{1, 3, 2, 4, 8, 6, 7, 2, 3, 0}
BubbleSort(values)
fmt.Println("sorted:", values)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment