Skip to content

Commit b30aec6

Browse files
committed
Implemented the bubble sort along with the written tutorial and created some boiler plate for the practice problems
1 parent 0f951fa commit b30aec6

File tree

4 files changed

+150
-0
lines changed
  • 001_bubble_sort

4 files changed

+150
-0
lines changed

001_bubble_sort/000_tutorial/main.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var numbers []int = []int{5, 4, 2, 3, 1, 0}
7+
fmt.Println("Unsorted:", numbers)
8+
9+
bubbleSort(numbers)
10+
fmt.Println("Sorted:", numbers)
11+
}
12+
13+
func bubbleSort(numbers []int) {
14+
var N int = len(numbers)
15+
var i int
16+
for i = 0; i < N; i++ {
17+
if !sweep(numbers, i) {
18+
return
19+
}
20+
}
21+
}
22+
23+
func sweep(numbers []int, prevPasses int) bool {
24+
var N int = len(numbers)
25+
var firstIndex int = 0
26+
var secondIndex int = 1
27+
var didSwap bool = false
28+
29+
for secondIndex < (N - prevPasses) {
30+
var firstNumber int = numbers[firstIndex]
31+
var secondNumber int = numbers[secondIndex]
32+
33+
if firstNumber > secondNumber {
34+
numbers[firstIndex] = secondNumber
35+
numbers[secondIndex] = firstNumber
36+
didSwap = true
37+
}
38+
39+
firstIndex++
40+
secondIndex++
41+
}
42+
return didSwap
43+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var numbers []int = []int{21, 4, 2, 13, 10, 0, 19, 11, 7, 5, 23, 18, 9, 14, 6, 8, 1, 20, 17, 3, 16, 22, 24, 15, 12}
7+
fmt.Println("Unsorted:", numbers)
8+
reverseBubbleSort(numbers)
9+
fmt.Println("Sorted: ", numbers)
10+
}
11+
12+
// This should sort numbers IN REVERSE ORDER!!!
13+
func reverseBubbleSort(numbers []int) {
14+
// Implement this and any other functions you may need
15+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package main
2+
3+
import "fmt"
4+
5+
func main() {
6+
var animals []string = []string{
7+
"dog",
8+
"cat",
9+
"alligator",
10+
"cheetah",
11+
"rat",
12+
"moose",
13+
"cow",
14+
"mink",
15+
"porcupine",
16+
"dung beetle",
17+
"camel",
18+
"steer",
19+
"bat",
20+
"hamster",
21+
"horse",
22+
"colt",
23+
"bald eagle",
24+
"frog",
25+
"rooster",
26+
}
27+
28+
fmt.Println("Unsorted:", animals)
29+
bubbleSort(animals)
30+
fmt.Println("Sorted: ", animals)
31+
}
32+
33+
func bubbleSort(animals []string) {
34+
// Implement this and any other functions you may need
35+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package main
2+
3+
import "fmt"
4+
5+
type User struct {
6+
FirstName string
7+
LastName string
8+
}
9+
10+
func main() {
11+
var users []User = []User{
12+
User{
13+
FirstName: "Jon",
14+
LastName: "Calhoun",
15+
},
16+
User{
17+
FirstName: "Bob",
18+
LastName: "Smith",
19+
},
20+
User{
21+
FirstName: "John",
22+
LastName: "Smith",
23+
},
24+
User{
25+
FirstName: "Larry",
26+
LastName: "Green",
27+
},
28+
User{
29+
FirstName: "Joseph",
30+
LastName: "Page",
31+
},
32+
User{
33+
FirstName: "George",
34+
LastName: "Costanza",
35+
},
36+
User{
37+
FirstName: "Jerry",
38+
LastName: "Seinfeld",
39+
},
40+
User{
41+
FirstName: "Shane",
42+
LastName: "Calhoun",
43+
},
44+
}
45+
46+
fmt.Println("Unsorted:", users)
47+
bubbleSort(users)
48+
fmt.Println("Sorted: ", users)
49+
}
50+
51+
func bubbleSort(users []User) {
52+
// Implement this and any other functions you may need
53+
// If you are writing Go code, you can access the first
54+
// and last name like so:
55+
fmt.Println(users[0].FirstName)
56+
fmt.Println(users[0].LastName)
57+
}

0 commit comments

Comments
 (0)