Created
October 15, 2018 12:52
-
-
Save sas1ni69/b857042dba15c2142e520443a6d17620 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 main | |
import ( | |
"reflect" | |
"testing" | |
) | |
func BenchmarkBubbleSortWrostCase(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
unsortedArray := []int{97, 96, 95, 93, 93, 92, | |
91, 89, 88, 87, 86, 84, 82, 82, 81, 81, 81, 80, 80, | |
79, 79, 79, 78, 78, 78, 78, 78, 78, 77, 76, 75, 74, | |
71, 70, 69, 68, 66, 66, 65, 64, 62, 61, 60, 60, 58, | |
58, 58, 57, 56, 56, 54, 53, 51, 50, 48, 48, 45, 45, | |
44, 44, 44, 43, 42, 42, 37, 37, 35, 35, 34, 32, 29, | |
29, 29, 28, 27, 26, 26, 24, 22, 22, 20, 20, 19, 18, | |
18, 18, 17, 15, 14, 12, 11, 11, 10, 9, 7, 5, 5, 4, 2, 2} | |
BubbleSort(unsortedArray) | |
} | |
} | |
func BenchmarkBubbleSortWithBestCase(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
unsortedArray := []int{2, 2, 4, 5, 5, 7, | |
9, 10, 11, 11, 12, 14, 15, 17, 18, 18, 18, | |
19, 20, 20, 22, 22, 24, 26, 26, 27, 28, 29, | |
29, 29, 32, 34, 35, 35, 37, 37, 42, 42, 43, | |
44, 44, 44, 45, 45, 48, 48, 50, 51, 53, 54, | |
56, 56, 57, 58, 58, 58, 60, 60, 61, 62, 64, | |
65, 66, 66, 68, 69, 70, 71, 74, 75, 76, 77, | |
78, 78, 78, 78, 78, 78, 79, 79, 79, 80, 80, | |
81, 81, 81, 82, 82, 84, 86, 87, 88, 89, 91, | |
92, 93, 93, 95, 96, 97} | |
BubbleSort(unsortedArray) | |
} | |
} | |
func BenchmarkOptimizedBubbleSortWorstCase(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
unsortedArray := []int{97, 96, 95, 93, 93, 92, | |
91, 89, 88, 87, 86, 84, 82, 82, 81, 81, 81, 80, 80, | |
79, 79, 79, 78, 78, 78, 78, 78, 78, 77, 76, 75, 74, | |
71, 70, 69, 68, 66, 66, 65, 64, 62, 61, 60, 60, 58, | |
58, 58, 57, 56, 56, 54, 53, 51, 50, 48, 48, 45, 45, | |
44, 44, 44, 43, 42, 42, 37, 37, 35, 35, 34, 32, 29, | |
29, 29, 28, 27, 26, 26, 24, 22, 22, 20, 20, 19, 18, | |
18, 18, 17, 15, 14, 12, 11, 11, 10, 9, 7, 5, 5, 4, 2, 2} | |
OptimizedBubbleSort(unsortedArray) | |
} | |
} | |
func BenchmarkOptimizedBubbleSortBestCase(b *testing.B) { | |
for n := 0; n < b.N; n++ { | |
unsortedArray := []int{2, 2, 4, 5, 5, 7, | |
9, 10, 11, 11, 12, 14, 15, 17, 18, 18, 18, | |
19, 20, 20, 22, 22, 24, 26, 26, 27, 28, 29, | |
29, 29, 32, 34, 35, 35, 37, 37, 42, 42, 43, | |
44, 44, 44, 45, 45, 48, 48, 50, 51, 53, 54, | |
56, 56, 57, 58, 58, 58, 60, 60, 61, 62, 64, | |
65, 66, 66, 68, 69, 70, 71, 74, 75, 76, 77, | |
78, 78, 78, 78, 78, 78, 79, 79, 79, 80, 80, | |
81, 81, 81, 82, 82, 84, 86, 87, 88, 89, 91, | |
92, 93, 93, 95, 96, 97} | |
OptimizedBubbleSort(unsortedArray) | |
} | |
} | |
func TestOptimizedBubbleSort(t *testing.T) { | |
unsortedArray := []int{3, 1, 7, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | |
sortedArray := []int{1, 3, 7, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | |
resultArray := OptimizedBubbleSort(unsortedArray) | |
if reflect.DeepEqual(sortedArray, resultArray) == false { | |
t.Errorf("OptimizedBubbleSort was incorrect, got: %d, want: %v.", resultArray, sortedArray) | |
} | |
} | |
func TestOptimizedBubbleSortWithCompletelyReversedSlice(t *testing.T) { | |
unsortedArray := []int{20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 7, 3, 1} | |
sortedArray := []int{1, 3, 7, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20} | |
resultArray := OptimizedBubbleSort(unsortedArray) | |
if reflect.DeepEqual(sortedArray, resultArray) == false { | |
t.Errorf("BubbleSort was incorrect, given: %d, received: %d.", unsortedArray, resultArray) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment