Last active
February 26, 2022 20:59
-
-
Save abdivasiyev/4b3b99f209412836d353109303c65825 to your computer and use it in GitHub Desktop.
Bubble sorting implementation golang
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
package sorting | |
// Bubble sorts the given slice of integers in ascending order with bubble sort algorithm. | |
func Bubble(numbers []int) { | |
size := len(numbers) | |
for i:=0; i<size; i++ { | |
for j:=0; j<i; j++ { | |
if numbers[j] > numbers[i] { | |
numbers[j], numbers[i] = numbers[i], numbers[j] | |
} | |
} | |
} | |
} |
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
package sorting | |
import "testing" | |
func TestBubble(t *testing.T) { | |
tests := []struct { | |
name string | |
numbers []int | |
expected []int | |
}{ | |
{ | |
name: "sort small numbers", | |
numbers: []int{3, 4, 2, 1}, | |
expected: []int{1, 2, 3, 4}, | |
}, | |
{ | |
name: "sort large numbers", | |
numbers: []int{10, 20, 1, 39, 100, -1, -10, -92}, | |
expected: []int{-92, -10, -1, 1, 10, 20, 39, 100}, | |
}, | |
} | |
for _, tt := range tests { | |
t.Run(tt.name, func(t *testing.T) { | |
Bubble(tt.numbers) | |
for i := 0; i < len(tt.numbers); i++ { | |
if tt.numbers[i] != tt.expected[i] { | |
t.Errorf("Expected %v, got %v", tt.expected, tt.numbers) | |
} | |
} | |
}) | |
} | |
} | |
// Benchmark test for Bubble with multiple and large inputs | |
func BenchmarkBubble(b *testing.B) { | |
tests := []struct { | |
name string | |
numbers []int | |
}{ | |
{ | |
name: "benchmark for 5 elements", | |
numbers: []int{3, 4, 2, 1, 5}, | |
}, | |
{ | |
name: "benchmark for 10 elements", | |
numbers: []int{10, 20, 1, 39, 100, -1, -10, -92, -100, -1000}, | |
}, | |
{ | |
name: "benchmark for 20 elements", | |
numbers: []int{ | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
}, | |
}, | |
{ | |
name: "benchmark for 1000 elements", | |
numbers: []int{ | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, 39, 100, -1, -10, -92, -100, -1000, | |
10, 20, 1, | |
}, | |
}} | |
for _, tt := range tests { | |
b.Run(tt.name, func(b *testing.B) { | |
for i := 0; i < b.N; i++ { | |
Bubble(tt.numbers) | |
} | |
}) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment