Skip to content

Commit 6ff8063

Browse files
committed
A rough first pass at the code, but there still needs to be a tutorial to go along with it
1 parent 83b0f2a commit 6ff8063

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
.DS_Store
2+
tmp/*
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
func main() {
9+
// First we are going to start by creating an array with
10+
// the numbers 0 - 10 that are in a psuedo-random order.
11+
// These aren't truly random becuase I am not seeding the
12+
// random package, so they end up being the same every
13+
// time we run our code, but they are not in sorted order.
14+
// This is actually really handy for cases like this since
15+
// everyone gets to see the exact same execution.
16+
var numbers []int = rand.Perm(10)
17+
18+
// Next we are going to print out the numbers just to
19+
// verify that they aren't sorted.
20+
// You should see the output: [9 4 2 6 8 0 3 1 7 5]
21+
fmt.Println(numbers)
22+
23+
// First we are going to focus on the most basic implementation possible. That means we won't be doing any of the optimizations we discussed in the article, but instead will just be writing code that follows the basic algorithm described.
24+
// To do this we are going to break it into two parts. The first is the code that compares every pair in the array and swaps two values if the first is greater than the second. This will be the function bubbleUp().
25+
// The second part we will write is the for loop that calls the code in the first part N times.
26+
bubbleSort(numbers)
27+
fmt.Println(numbers)
28+
}
29+
30+
// The purpose of this function is to compare every consecutive pair of numbers and swap them if the first is greater than the second. In essence it is bubbling up the largest number that isn't in its final position with each pass.
31+
// NOTE: Slices (similar to arrays in other langauges, but not exactly the same) are passed by reference in Go, so any changes we make to this array will be propogated to the original slice. You could just as easily return the array when this function finishes executing, but we won't be.
32+
func bubbleUp(numbers []int) {
33+
// len(numbers) - 1 becuase we will look at the number at i and i+1, so we by setting our max as less than (not equal to) len(numbers) - 1 we ensure that we dont go out of bounds of our array.
34+
for i := 0; i < len(numbers)-1; i++ {
35+
// Get the pair of numbers that we want to look at.
36+
var first int = numbers[i]
37+
var second int = numbers[i+1]
38+
// Check to see if the first number in the pair is greater than the second.
39+
if first > second {
40+
// If it is, we need to swap the numbers.
41+
numbers[i] = second
42+
numbers[i+1] = first
43+
}
44+
}
45+
}
46+
47+
// The purpose of this function is sort the numbers slice by iterating N times (where N is the size of the slice) and calling the bubbleUp function on each iteration.
48+
func bubbleSort(numbers []int) {
49+
for i := 0; i < len(numbers); i++ {
50+
bubbleUp(numbers)
51+
}
52+
}

001_bubble_sort/002_optimized/main.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"math/rand"
6+
)
7+
8+
func main() {
9+
var numbers []int = rand.Perm(10)
10+
fmt.Println(numbers)
11+
bubbleSort(numbers)
12+
fmt.Println(numbers)
13+
}
14+
15+
func bubbleUp(numbers []int, n int) bool {
16+
var swapped bool = false
17+
for i := 0; i < n-1; i++ {
18+
var first int = numbers[i]
19+
var second int = numbers[i+1]
20+
if first > second {
21+
numbers[i] = second
22+
numbers[i+1] = first
23+
swapped = true
24+
}
25+
}
26+
return swapped
27+
}
28+
29+
func bubbleSort(numbers []int) {
30+
for i := 0; i < len(numbers); i++ {
31+
// Terminate early if bubbleUp doesn't swap anything, and reduce the max index we look at by len(numbers) - i
32+
if !bubbleUp(numbers, len(numbers)-i) {
33+
return
34+
}
35+
}
36+
}

0 commit comments

Comments
 (0)