Skip to content

Instantly share code, notes, and snippets.

@RavenZZ
Forked from RichardKnop/bubble_sort.go
Created July 12, 2017 03:29
Show Gist options
  • Save RavenZZ/195fa7d931699e019d91bac729afaa61 to your computer and use it in GitHub Desktop.
Save RavenZZ/195fa7d931699e019d91bac729afaa61 to your computer and use it in GitHub Desktop.
Bubble Sort
package main
import (
"fmt"
)
func main() {
items := []int{4, 202, 3, 9, 6, 5, 1, 43, 506, 2, 0, 8, 7, 100, 25, 4, 5, 97, 1000, 27}
bubbleSort(items)
fmt.Println(items)
}
func bubbleSort(items []int) {
var (
n = len(items)
swapped = true
)
for swapped {
swapped = false
for i := 0; i < n-1; i++ {
if items[i] > items[i+1] {
items[i+1], items[i] = items[i], items[i+1]
swapped = true
}
}
n = n - 1
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment