|
| 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 | +} |
0 commit comments