-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from Shevchenko-26/master
added cocktail sorting with a couple of tests
- Loading branch information
Showing
3 changed files
with
122 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# Cocktail Sort | ||
Cocktail Sort is a variation of Bubble sort. The Bubble sort algorithm always traverses elements from left and moves the largest element to its correct position in first iteration and second largest in second iteration and so on. | ||
|
||
### Example | ||
``` | ||
Array: [1 5 4 2 8 0 2] | ||
###### first iteration : | ||
gets biggest element to the last position: | ||
gets smallest element to the first position and so on: | ||
First Forward Pass | ||
[1 5 4 2 8 0 2] | ||
[1 4 5 2 8 0 2] | ||
[1 4 2 5 8 0 2] | ||
[1 4 2 5 8 0 2] | ||
[1 4 2 5 0 8 2] | ||
[1 4 2 5 0 2 8] | ||
First Backward Pass: | ||
[1 4 2 5 0 2 8] | ||
[1 4 2 0 5 2 8] | ||
[1 4 0 2 5 2 8] | ||
[1 0 4 2 5 2 8] | ||
[0 1 4 2 5 2 8] | ||
###### iteration 2: | ||
Forward Pass | ||
[0 1 4 2 5 2 8] | ||
[0 1 4 2 5 2 8] | ||
[0 1 2 4 5 2 8] | ||
[0 1 2 4 5 2 8] | ||
[0 1 2 4 2 5 8] | ||
Backward Pass: | ||
[0 1 2 2 4 5 8] | ||
[0 1 2 2 4 5 8] | ||
[0 1 2 2 4 5 8] | ||
[0 1 2 2 4 5 8] | ||
###### iteration 3: | ||
[0 1 2 2 4 5 8] | ||
[0 1 2 2 4 5 8] | ||
[0 1 2 2 4 5 8] | ||
[0 1 2 2 4 5 8] | ||
Since array is already sorted, Backward Pass is not conducted | ||
``` | ||
|
||
### Complexity | ||
``` | ||
Worst Case Time Complexity: O(n^2) | ||
Best Case Time Complexity: O(n) when it's already sorted | ||
Average Time Complexity: O(n^2) | ||
``` |
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,36 @@ | ||
package sort | ||
|
||
import "fmt" | ||
|
||
//CocktailSort ... Basically two sided bubble sort | ||
func CocktailSort(list []int) []int { | ||
swapped := true | ||
start, end := 0, len(list)-1 | ||
for swapped { | ||
swapped = false | ||
fmt.Println() | ||
|
||
for i := start; i < end; i++ { | ||
if list[i] > list[i+1] { | ||
list[i], list[i+1] = list[i+1], list[i] | ||
swapped = true | ||
} | ||
fmt.Println(list) | ||
} | ||
fmt.Println() | ||
if !swapped { | ||
break | ||
} | ||
swapped = false | ||
end-- | ||
for i := end - 1; i >= start; i-- { | ||
if list[i] > list[i+1] { | ||
list[i], list[i+1] = list[i+1], list[i] | ||
swapped = true | ||
} | ||
fmt.Println(list) | ||
} | ||
|
||
} | ||
return list | ||
} |
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,27 @@ | ||
package sort | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
//TestCocktailSort ... Test if the program actually sorts array or not | ||
func TestCocktailSort(t *testing.T) { | ||
a := [][]int{ | ||
{5, 1, 4, 2, 8, 0, 2}, | ||
{0, -1, 2, 2, -4, 5, -8}, | ||
{12, 90, 23, 87, 32, 67, 27}, | ||
} | ||
result := [][]int{ | ||
{0, 1, 2, 2, 4, 5, 8}, | ||
{-8, -4, -1, 0, 2, 2, 5}, | ||
{12, 23, 27, 32, 67, 87, 90}, | ||
} | ||
for i := range a { | ||
sorted := CocktailSort(a[i]) | ||
|
||
assert.Equal(t, result[i], sorted, "Oops.. cocktail sort didn't work!!") | ||
|
||
} | ||
|
||
} |