Skip to content

Commit 0f951fa

Browse files
committed
Cleaned up the comments a bit to linewrap manually. This makes the code read a bit better on sites like github and the go playground.
1 parent 6ff8063 commit 0f951fa

File tree

1 file changed

+36
-8
lines changed
  • 001_bubble_sort/001_first_pass

1 file changed

+36
-8
lines changed

001_bubble_sort/001_first_pass/main.go

Lines changed: 36 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,48 @@ func main() {
2020
// You should see the output: [9 4 2 6 8 0 3 1 7 5]
2121
fmt.Println(numbers)
2222

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.
23+
// First we are going to focus on the most basic
24+
// implementation possible. That means we won't be doing
25+
// any of the optimizations we discussed in the article,
26+
// but instead will just be writing code that follows the
27+
// basic algorithm described.
28+
//
29+
// To do this we are going to break it into two parts.
30+
// The first is the code that compares every pair in the
31+
// array (slice in Go, but roughly the same) and swaps
32+
// two values if the first is greater than the second.
33+
// This will be the function bubbleUp().
34+
//
35+
// The second part we will write is the for loop that
36+
// calls the code in the first part N times. This will be
37+
// the bubbleSort() function that we call here.
2638
bubbleSort(numbers)
2739
fmt.Println(numbers)
2840
}
2941

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.
42+
// The purpose of this function is to compare every
43+
// consecutive pair of numbers and swap them if the first
44+
// is greater than the second. In essence it is bubbling up
45+
// the largest number that isn't in its final position with
46+
// each pass.
47+
//
48+
// NOTE: Slices (similar to arrays in other langauges, but
49+
// not exactly the same) are passed by reference in Go, so
50+
// any changes we make to this array will be propogated to
51+
// the original slice. You could just as easily return the
52+
// array when this function finishes executing, but we won't
53+
// be.
3254
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.
55+
// len(numbers) - 1 becuase we will look at the number at
56+
// i and i+1, so we by setting our max as less than (not
57+
// equal to) len(numbers) - 1 we ensure that we dont go
58+
// out of bounds of our array.
3459
for i := 0; i < len(numbers)-1; i++ {
3560
// Get the pair of numbers that we want to look at.
3661
var first int = numbers[i]
3762
var second int = numbers[i+1]
38-
// Check to see if the first number in the pair is greater than the second.
63+
// Check to see if the first number in the pair is
64+
// greater than the second.
3965
if first > second {
4066
// If it is, we need to swap the numbers.
4167
numbers[i] = second
@@ -44,7 +70,9 @@ func bubbleUp(numbers []int) {
4470
}
4571
}
4672

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.
73+
// The purpose of this function is sort the numbers slice
74+
// by iterating N times (where N is the size of the slice)
75+
// and calling the bubbleUp function on each iteration.
4876
func bubbleSort(numbers []int) {
4977
for i := 0; i < len(numbers); i++ {
5078
bubbleUp(numbers)

0 commit comments

Comments
 (0)