Skip to content

Commit

Permalink
feat: Bubble Sort added
Browse files Browse the repository at this point in the history
  • Loading branch information
aholbreich committed Nov 9, 2024
1 parent 9235cf7 commit ad91c56
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 17 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,10 @@ Browse and learn. Feel free to fork, to provide improvements or comments.
To run tests use:

```
go test
go test -v ./...
```
Check coverage with

```
go test -cover ./...
```
16 changes: 1 addition & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
module go_katas

go 1.22.3
go 1.23.2

require (
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.33.1
)

require (
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/nxadm/tail v1.4.8 // indirect
golang.org/x/net v0.24.0 // indirect
golang.org/x/sys v0.19.0 // indirect
golang.org/x/text v0.14.0 // indirect
gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
5 changes: 4 additions & 1 deletion perfectnumber_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ func TestFindPerfectNumbers(t *testing.T) {

func TestFindPerfectNumbersEfficient(t *testing.T) {

result := FindPerfectNumbersEfficient(5)
result := FindPerfectNumbersEfficient(8)
expected := []*big.Int{
big.NewInt(6),
big.NewInt(28),
big.NewInt(496),
big.NewInt(8128),
big.NewInt(33550336),
big.NewInt(8589869056),
big.NewInt(137438691328),
big.NewInt(2305843008139952128),
}
if !reflect.DeepEqual(result, expected) {
t.Errorf("FindPerfectNumbersEfficient(%d) = %v, expected %v", 1, result, expected)
Expand Down
21 changes: 21 additions & 0 deletions sorting/bubblesort.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package sorting

// O(n2)
func BubbleSort(arr []int) {
n := len(arr)
for i := 0; i < n-1; i++ {
swapped := false
for j := 0; j < n-1-i; j++ {
if arr[j] > arr[j+1] {

arr[j], arr[j+1] = arr[j+1], arr[j]
swapped = true
}
}

if !swapped {
// if no swap occurred on the whole series run, everything is sorted
break
}
}
}
33 changes: 33 additions & 0 deletions sorting/bubblesort_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package sorting

import (
"fmt"
"reflect"
"testing"
"time"
)

func TestBubbleSort(t *testing.T) {

//Approx the 1 mb array
const arraySize int = 131072 * 1

var arr [arraySize]int
var expected [arraySize]int

for i := 0; i < arraySize; i++ {
arr[i] = arraySize - 1 - i
}

for i := 0; i < arraySize; i++ {
expected[i] = i
}

start := time.Now()
BubbleSort(arr[:])
elapsed := time.Since(start)
fmt.Printf("Bubble sort: Time for 100mb monotone int array %s\n", elapsed)
if !reflect.DeepEqual(arr, expected) {
t.Errorf("BubbleSort failed. Got %v, expected %v", arr, expected)
}
}

0 comments on commit ad91c56

Please sign in to comment.