Skip to main content

Go Bubbles

Shenzhen, China

Github

Bubble Sorting through Slices in Go

package main

import (
"fmt"
)

// takes slice of integers and bubble sorts them
func BubbleSort(input []int) []int {
n := len(input)
swapped := true

for swapped {
swapped = false

// walk through input slice
for i := 0; i < n-1; i++ {

// if element i is bigger than element i+1 swap them
if input[i] > input[i+1] {

fmt.Println("Bubble Up!", input[i])

input[i], input[i+1] = input[i+1], input[i]

swapped = true
}
}
}
return input
}

func main() {
fmt.Println("Go Bubble Sort")

unsortedInput := []int{8, 2, 9, 3, 1, 6, 7}
fmt.Println("Input: ", unsortedInput)

sortedOutput := BubbleSort(unsortedInput)
fmt.Println("Output: ", sortedOutput)
}
go run ./main.go

Go Bubble Sort
Input: [8 2 9 3 1 6 7]
Bubble Up! 8
Bubble Up! 9
Bubble Up! 9
Bubble Up! 9
Bubble Up! 9
Bubble Up! 8
Bubble Up! 8
Bubble Up! 8
Bubble Up! 8
Bubble Up! 3
Bubble Up! 2
Output: [1 2 3 6 7 8 9]