You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 001_bubble_sort/001_first_pass/main.go
+36-8Lines changed: 36 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -20,22 +20,48 @@ func main() {
20
20
// You should see the output: [9 4 2 6 8 0 3 1 7 5]
21
21
fmt.Println(numbers)
22
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.
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.
26
38
bubbleSort(numbers)
27
39
fmt.Println(numbers)
28
40
}
29
41
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.
32
54
funcbubbleUp(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.
34
59
fori:=0; i<len(numbers)-1; i++ {
35
60
// Get the pair of numbers that we want to look at.
36
61
varfirstint=numbers[i]
37
62
varsecondint=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.
39
65
iffirst>second {
40
66
// If it is, we need to swap the numbers.
41
67
numbers[i] =second
@@ -44,7 +70,9 @@ func bubbleUp(numbers []int) {
44
70
}
45
71
}
46
72
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.
0 commit comments