Golang ProgramsGolang Programs

Golang program for implementation of Bubble Sort

Given an unordered list, we compare adjacent elements in the list, each time, putting in the right order of magnitude, only two elements. The algorithm hinges on a swap procedure. Knowing how many times to swap is important when implementing a bubble sort algorithm. To sort a list of numbers such as [3, 2, 1], we need to swap the elements a maximum of twice. This is equal to the length of the list minus 1
// Bubble Sort in Golang
package main

import (
	"fmt"
	"math/rand"
	"time"
)

func main() {

	slice := generateSlice(20)
	fmt.Println("\n--- Unsorted --- \n\n", slice)
	bubblesort(slice)
	fmt.Println("\n--- Sorted ---\n\n", slice, "\n")
}

// Generates a slice of size, size filled with random numbers
func generateSlice(size int) []int {

	slice := make([]int, size, size)
	rand.Seed(time.Now().UnixNano())
	for i := 0; i < size; i++ {
		slice[i] = rand.Intn(999) - rand.Intn(999)
	}
	return slice
}
 
func bubblesort(items []int) {
    var (
        n = len(items)
        sorted = false
    )
    for !sorted {
        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
            }
        }
        if !swapped {
            sorted = true
        }
        n = n - 1
    }
}
C:\golang\time>go run sort.go

--- Unsorted ---

[-129 755 38 354 248 6 -160 212 -184 336 -85 222 776 587 490 -503 -420 -54 -502 -341]

--- Sorted ---

[-503 -502 -420 -341 -184 -160 -129 -85 -54 6 38 212 222 248 336 354 490 587 755 776]


C:\golang\time>