devpapa

Bubblesort in Go

This is what a simple unoptimised bubblesort program looks like in Go.

package main

import (
	"fmt"
)

// unoptimised bubblesort 
func bsort(arr []int) []int {
	temp := 0
	arrlen := len(arr)
	for i := 0; i < arrlen; i++ {
		for j := 0; j < arrlen-i-1; j++ {
			if arr[j] > arr[j+1] {
				temp = arr[j]
				arr[j] = arr[j+1]
				arr[j+1] = temp
			}
		}
	}
	return arr
}

func main() {
	arr := []int{64, 34, 25, 12, 22, 11, 90}
	fmt.Println("unsorted array:", arr)
	fmt.Println("sorted array:", bsort(arr))
}

I've been playing with Go a lot lately and I think I'm in love.

#go