Skip to content

Commit 9627cfb

Browse files
committed
Migrate doctest for CocktailShakerSort.js
(also remove inline test driver code)
1 parent 3eff8fd commit 9627cfb

File tree

2 files changed

+18
-29
lines changed

2 files changed

+18
-29
lines changed

Sorts/CocktailShakerSort.js

Lines changed: 5 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,14 @@
1-
/*
1+
/**
22
* Cocktail Shaker Sort is an algorithm that is a Bidirectional Bubble Sort.
3+
*
34
* The algorithm extends bubble sort by operating in two directions.
4-
* While it improves on bubble sort by more quickly moving items to the beginning of the list,
5-
* it provides only marginal performance improvements.
5+
* While it improves on bubble sort by more quickly moving items to the beginning of the list, it provides only marginal
6+
* performance improvements.
67
*
78
* Wikipedia (Cocktail Shaker Sort): https://en.wikipedia.org/wiki/Cocktail_shaker_sort
89
* Wikipedia (Bubble Sort): https://en.wikipedia.org/wiki/Bubble_sort
9-
*
10-
*/
11-
12-
/**
13-
* Doctests
14-
*
15-
* > cocktailShakerSort([5, 4, 1, 2, 3])
16-
* [1, 2, 3, 4, 5]
17-
* > cocktailShakerSort([])
18-
* []
19-
* > cocktailShakerSort([1, 2, 3])
20-
* [1, 2, 3]
2110
*/
22-
23-
function cocktailShakerSort (items) {
11+
export function cocktailShakerSort (items) {
2412
for (let i = items.length - 1; i > 0; i--) {
2513
let j
2614

@@ -41,15 +29,3 @@ function cocktailShakerSort (items) {
4129

4230
return items
4331
}
44-
45-
/**
46-
* Implementation of Cocktail Shaker Sort
47-
*/
48-
const array = [5, 6, 7, 8, 1, 2, 12, 14]
49-
// Before Sort
50-
console.log('\n- Before Sort | Implementation of Cocktail Shaker Sort -')
51-
console.log(array)
52-
// After Sort
53-
console.log('- After Sort | Implementation of Cocktail Shaker Sort -')
54-
console.log(cocktailShakerSort(array))
55-
console.log('\n')

Sorts/test/CocktailShakerSort.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import { cocktailShakerSort } from '../CocktailShakerSort'
2+
3+
describe('CocktailShakerSort', () => {
4+
it('should sort arrays correctly', () => {
5+
expect(cocktailShakerSort([5, 4, 1, 2, 3])).toEqual([1, 2, 3, 4, 5])
6+
expect(cocktailShakerSort([1, 2, 3])).toEqual([1, 2, 3])
7+
expect(cocktailShakerSort([5, 6, 7, 8, 1, 2, 12, 14])).toEqual([1, 2, 5, 6, 7, 8, 12, 14])
8+
})
9+
10+
it('should work for empty arrays, too', () => {
11+
expect(cocktailShakerSort([])).toEqual([])
12+
})
13+
})

0 commit comments

Comments
 (0)