Skip to content

Commit

Permalink
Merge pull request #6 from Shevchenko-26/master
Browse files Browse the repository at this point in the history
added cocktail sorting with a couple of tests
  • Loading branch information
ektagarg authored Oct 26, 2019
2 parents 8172d74 + affc985 commit a069131
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
59 changes: 59 additions & 0 deletions array/sort/cocktail_sort/Readme.md
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)
```
36 changes: 36 additions & 0 deletions array/sort/cocktail_sort/cocktail_sort.go
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
}
27 changes: 27 additions & 0 deletions array/sort/cocktail_sort/cocktail_sort_test.go
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!!")

}

}

0 comments on commit a069131

Please sign in to comment.