Skip to content

Instantly share code, notes, and snippets.

@andresabello
Created June 6, 2020 16:40
Show Gist options
  • Save andresabello/b0fa98e186d18615b2292f5767eb1933 to your computer and use it in GitHub Desktop.
Save andresabello/b0fa98e186d18615b2292f5767eb1933 to your computer and use it in GitHub Desktop.
Bubble sort algorithm
package main
import (
"fmt"
)
func main() {
unordered := []int{99, 6, 5, 88, 3, 9, 7, 11}
ordered := bubbleSort(unordered)
fmt.Println(unordered)
fmt.Println(ordered)
}
func bubbleSort(numbers []int) []int {
length := len(numbers)
for i := 0; i < length; i++ {
for j := 0; j < length; j++ {
if j+1 <= length-1 && numbers[j] > numbers[j+1] {
temp := numbers[j]
numbers[j] = numbers[j+1]
numbers[j+1] = temp
}
}
}
return numbers
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment