Skip to content

Commit 410bcf9

Browse files
committed
bubble sort
1 parent c81dd54 commit 410bcf9

File tree

6 files changed

+122
-1
lines changed

6 files changed

+122
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.idea
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package main
2+
3+
import (
4+
"bufio"
5+
"fmt"
6+
"os"
7+
"strconv"
8+
"strings"
9+
)
10+
11+
func Swap(integers []int, i int) {
12+
integers[i], integers[i+1] = integers[i+1], integers[i]
13+
}
14+
15+
func BubbleSort(integers []int) {
16+
N := len(integers)
17+
for i := 0; i < N; i++ {
18+
for j := 0; j < N-i-1; j++ {
19+
if integers[j] > integers[j+1] {
20+
Swap(integers, j)
21+
}
22+
}
23+
}
24+
}
25+
26+
func ScanIntegers() []int {
27+
fmt.Println("Enter up to ten integers separated by spaces:")
28+
reader := bufio.NewReader(os.Stdin)
29+
input, _ := reader.ReadString('\n')
30+
input = strings.TrimSpace(input)
31+
strIntegers := strings.Fields(input)
32+
33+
integers := make([]int, 0, len(strIntegers))
34+
35+
for _, str := range strIntegers {
36+
num, err := strconv.Atoi(str)
37+
if err != nil {
38+
fmt.Printf("Invalid integer: %s\n", str)
39+
continue
40+
}
41+
integers = append(integers, num)
42+
}
43+
44+
return integers
45+
}
46+
47+
func main() {
48+
tenIntegers := ScanIntegers()
49+
BubbleSort(tenIntegers)
50+
fmt.Print(tenIntegers)
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"reflect"
7+
"testing"
8+
)
9+
10+
func TestBubbleSort(t *testing.T) {
11+
integers := []int{5, 2, 9, 1, -7, 3, 8, 4, 6, 10}
12+
expected := []int{-7, 1, 2, 3, 4, 5, 6, 8, 9, 10}
13+
14+
BubbleSort(integers)
15+
16+
if !reflect.DeepEqual(integers, expected) {
17+
t.Errorf("BubbleSort failed. Expected %v, got %v", expected, integers)
18+
}
19+
}
20+
21+
func TestSwap(t *testing.T) {
22+
integers := []int{1, 2, 3, 4, 5}
23+
index := 2
24+
expected := []int{1, 2, 4, 3, 5}
25+
26+
Swap(integers, index)
27+
28+
if !reflect.DeepEqual(integers, expected) {
29+
t.Errorf("Swap failed. Expected %v, got %v", expected, integers)
30+
}
31+
}
32+
33+
func TestScanTenIntegers(t *testing.T) {
34+
input := "5 2 9 1 7 3 8 4 6 10"
35+
expected := []int{5, 2, 9, 1, 7, 3, 8, 4, 6, 10}
36+
37+
// Redirect standard input
38+
oldStdin := os.Stdin
39+
defer func() {
40+
os.Stdin = oldStdin
41+
}()
42+
43+
r, w, _ := os.Pipe()
44+
os.Stdin = r
45+
46+
// Write input to stdin
47+
go func() {
48+
fmt.Fprintln(w, input)
49+
w.Close()
50+
}()
51+
52+
result := ScanIntegers()
53+
if !reflect.DeepEqual(result, expected) {
54+
t.Errorf("ScanIntegers failed. Expected %v, got %v", expected, result)
55+
}
56+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module bubble-sort
2+
3+
go 1.20
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Promgramming with Google Go Specialization
2+
3+
* Getting Started with Go: Learn the basics of Go
4+
* Functions, Method, and Interfaces in Go
5+
* Concurrency in Go: Explore the roles of channels and goroutines in implementing concurrency
6+
7+
## Programming with TDD
8+
9+
10+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
This repository documents my journey as a Java programmer to learn Golang
44

5-
1. **Complete the [Programming with Google Go Specialization](https://www.coursera.org/specializations/google-golang) on Coursera** [README](https://github.com/meowpunch/programming-with-golang/blob/main/Programming%20with%20Google%20Go%20Specialization/README.md): This course covers the basics of Golang, including functions, methods, interfaces, and concurrency. I will also practice Test-Driven Development (TDD) by completing the assignments in the [Learn Go With Tests](https://quii.gitbook.io/learn-go-with-tests/) book.
5+
1. **Complete the [Programming with Google Go Specialization](https://www.coursera.org/specializations/google-golang) on Coursera** [[README](https://github.com/meowpunch/programming-with-golang/blob/main/Programming%20with%20Google%20Go%20Specialization/README.md)]: This course covers the basics of Golang, including functions, methods, interfaces, and concurrency. I will also practice Test-Driven Development (TDD) by completing the assignments in the [Learn Go With Tests](https://quii.gitbook.io/learn-go-with-tests/) book.
66

77
2. **Delve into the LBC codebase with practical and advanced resources**:
88

0 commit comments

Comments
 (0)