Skip to content

Instantly share code, notes, and snippets.

@emre
Created August 21, 2013 12:41
Show Gist options
  • Save emre/6293953 to your computer and use it in GitHub Desktop.
Save emre/6293953 to your computer and use it in GitHub Desktop.
bubblesort in golang
package main
import "fmt"
func main() {
x := []int{
48, 96, 86, 68,
57, 82, 63, 70,
37, 34, 83, 27,
19, 97, 9, 17,
}
end := len(x) - 1
for {
if end == 0 {
break
}
for i := 0; i < len(x)-1; i++ {
if x[i] > x[i+1] {
x[i], x[i+1] = x[i+1], x[i]
}
}
end -= 1
}
fmt.Println(x)
}
@mysterytree
Copy link

mysterytree commented Mar 23, 2019

func bubbleSort(array []int) {
	for i := 0; i < len(array); i++ {
		for j := 0; j < len(array)-1-i; j++ {
			if array[j] > array[j+1] {
				array[j], array[j+1] = array[j+1], array[j]
			}
		}
	}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment