Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cocktail sort #273

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions sorts/cocktial_sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* @author codeme254 (Dennis Otwoma)
* @description
* Cocktail sort is meant to be an improvement of the
* bubble sort algorithm, it bubbles bigger elements
* to the end of the list and once at the end of the list,
* it bubbles smaller elements to the start of the list.
* The algorithm keeps swapping elements until all the
* elements are where they are supposed to be, if at any
* given pass no swapping happens, it means the array is sorted.
* @example
* const array = [32, 87, 14, 57, 60, 2, 72, 90, 45, 30]
* cocktailSort([32, 87, 14, 57, 60, 2, 72, 90, 45, 30])
*/

export const cocktailSort = (array: number[]): number[] => {
if (array.length <= 0) return array
let sortedLeft = 0
let sortedRight = array.length - 1

while (sortedLeft <= sortedRight) {
let i = sortedLeft
let swapHappened = false
while (i < sortedRight) {
if (array[i] > array[i + 1]) {
[array[i], array[i + 1]] = [array[i + 1], array[i]]
swapHappened = true
}
i += 1
}

// if no swap happened in the forward pass, it means the array is sorted
if (swapHappened === false) return array

swapHappened = false
while (i > sortedLeft) {
if (array[i] < array[i - 1]) {
[array[i], array[i - 1]] = [array[i - 1], array[i]]
swapHappened = true
}
i -= 1
}

// if no swap happened in the backwards pass, it means the array is sorted
if (swapHappened === false) return array

sortedLeft += 1
sortedRight -= 1
}
return array
}
47 changes: 47 additions & 0 deletions sorts/test/cocktail_sort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { cocktailSort } from '../cocktial_sort'

const testCases = [
[
[5, 2, 9, 1, 3],
[1, 2, 3, 5, 9]
],
[
[9, 8, 5, 1, 0],
[0, 1, 5, 8, 9]
],
[[], []],
[[3], [3]],
[
[32, 87, 14, 57, 60, 2, 72, 90, 45, 30],
[2, 14, 30, 32, 45, 57, 60, 72, 87, 90]
],
[
[1, 2, 5, 9, 12],
[1, 2, 5, 9, 12]
],
[
[3, 1, 2, 3, 1],
[1, 1, 2, 3, 3]
],
[
[7, 7, 7, 7],
[7, 7, 7, 7]
],
[
[-3, -1, -4, -2, -5],
[-5, -4, -3, -2, -1]
],
[
[-2, 4, -1, 3, 0],
[-2, -1, 0, 3, 4]
],
[
[1, 100, 2, 99, 3, 98],
[1, 2, 3, 98, 99, 100]
]
]

it.each(testCases)('%p sorted is %p', (input, expected) => {
const sorted = cocktailSort(input)
expect(sorted).toEqual(expected)
})