Writing Bubble Sort in Go from Scratch
Bubble sort is named for the way elements “bubble up” to the top of the list. Bubble sort repeatedly steps through a slice and compares adjacent elements, swapping them if they are out of order. It continues to loop over the slice until the whole list is completely sorted.
Full example of the bubble sort algorithm
func bubbleSort(input []int) []int {
swapped := true
for swapped {
swapped = false
for i := 1; i < len(input); i++ {
if input[i-1] > input[i] {
input[i], input[i-1] = input[i-1], input[i]
swapped = true
}
}
}
return input
}
Using the algorithm in code
func main() {
unsorted := []int{10, 6, 2, 1, 5, 8, 3, 4, 7, 9}
sorted := bubbleSort(unsortedInput)
// sorted = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
}
Why use bubble sort?
Bubble sort is famous for how easy it is to write. It’s one of the slowest sorting algorithms, but can be useful for a quick script or when the amount of data to be sorted is guaranteed to be small. If you need a sorting algorithm to use in a production system, I recommend not reinventing the wheel and using the built-in sort.Sort method.
Bubble sort Big-O complexity
While bubble sort is considered fast and easy to write, it’s actually one of the slowest sorting algorithms out there. Because bubble sort needs to move through the entire list for each element in the list, which in code is a nested for-loop, bubble sort has a complexity of O(n^2).
Related Articles
How to Properly Use Defer in Golang
Jun 01, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
What is the “defer” keyword in Go? In the Go programming language, defer is a keyword that allows developers to delay the execution of a function until the current function returns. What throws some people off is that the deferred function’s arguments are evaluated immediately, but the function itself doesn’t fire until the wrapping function exits.
Comprehensive Guide to Dates and Times in Go
May 17, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
Keeping track of time in code has long been every developer’s nightmare. While no language or package manages time perfectly, I think Golang does a pretty good job out-of-the-box. This full tutorial should answer ~90% of the questions you’ll have about time management in Go.
Concatenating with strings.Builder Quickly in Golang
May 04, 2021 by Lane Wagner - Boot.dev co-founder and backend engineer
The Go standard library makes concatenating strings easy. Concatenation is just a fancy word for adding strings together to make a larger string. For example, if we concatenate "hello", " " and "world" we’d get "hello world".
Golang vs Python: Which Language is Best For You?
Apr 29, 2021 by Zulie Rane - Data analysis and computer science techincal author
These two coding languages duke it out - but who’s the winner? In a world where the ability to write any code at all is a tremendous advantage, often the biggest problem coders face is knowing which language to start learning, rather than whether to learn one at all. There are different languages for just about every purpose you could think of. Of those popular coding languages, programmers often face an intense battle of Golang vs Python. (The official name is Go, but the website is Golang.org, so programmers typically refer to it as either interchangeably.)