-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9235cf7
commit ad91c56
Showing
5 changed files
with
65 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |