From 22f2a9a84ca9d407563b84a91b95371db0ab7c4a Mon Sep 17 00:00:00 2001 From: Im-Not-God <82208147+Im-Not-God@users.noreply.github.com> Date: Mon, 8 Aug 2022 21:19:15 +0800 Subject: [PATCH 1/7] Create a Cocktail Shaker Sort Algorithm This is my first attempt of the Cocktail Shaker Sort Algorithm, feel free to check and improve it. --- .../CocktailShakerSort.js | 55 ++++++++++++++++ .../sorting/cocktail-shaker-sort/README.md | 23 +++++++ .../cocktail-shaker-sort/README.zh-CN.md | 23 +++++++ .../__test__/CocktailShakerSort.test.js | 64 +++++++++++++++++++ 4 files changed, 165 insertions(+) create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/README.md create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js diff --git a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js new file mode 100644 index 00000000..a0eb57eb --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js @@ -0,0 +1,55 @@ +import Sort from '../Sort'; + +export default class CocktailShakerSort extends Sort { + sort(originalArray) { + // Flag that holds info about whether the swap has occur or not. + let swapped = true; + // Clone original array to prevent its modification. + const array = [...originalArray]; + + let start = 0; + let end = array.length; + + while (swapped) { + // reset the swapped flag on entering the loop, because it might be true from a previous iteration. + swapped = false; + + // loop forward same as the bubble sort + for (let i = start; i < end - 1; i++) { + // Call visiting callback. + this.callbacks.visitingCallback(array[i]); + + if (this.comparator.greaterThan(array[i], array[i + 1])) { + [array[i], array[i + 1]] = [array[i + 1], array[i]]; + swapped = true; + } + } + + // move the end point backward by one, because the item at the end point is already in its correct position. + end --; + + // if nothing swapped, then array is sorted. + if (swapped == false) + break; + + // reset the swapped flag so that it can be used in the next stage + swapped = false; + + // loop backward, doing the same comparison as in the previous stage + for (let i = end - 1; i >= start; i--) { + // Call visiting callback. + this.callbacks.visitingCallback(array[i]); + + if (this.comparator.greaterThan(array[i], array[i + 1])) { + [array[i], array[i + 1]] = [array[i + 1], array[i]]; + swapped = true; + } + } + + // move the start point forward by one, because the item at the start point is already in its correct position. + start ++; + + } + return array; + } +} diff --git a/src/algorithms/sorting/cocktail-shaker-sort/README.md b/src/algorithms/sorting/cocktail-shaker-sort/README.md new file mode 100644 index 00000000..11769e26 --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/README.md @@ -0,0 +1,23 @@ +# Cocktail Shaker Sort + +_Read this in other languages:_ +[_简体中文_](README.zh-CN.md) + +Cocktail Shaker Sort is an algorithm that is a Bidirectional Bubble Sort. + +The algorithm extends bubble sort by operating in two directions. +While it improves on bubble sort by more quickly moving items to the beginning of the list, it provides only marginal performance improvements. + +![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif) + +## Complexity + +| Name | Best | Average | Worst | Memory | Stable | Comments | +| ------------------------------ | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | +| **Cocktail Shaker Sort** | n | n2 | n2 | 1 | Yes | | + +## References + +- [Cocktail Shaker Sort on Wikipedia](https://en.wikipedia.org/wiki/Cocktail_shaker_sort) + +- [Bubble Sort on Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort) diff --git a/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md b/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md new file mode 100644 index 00000000..2101a911 --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md @@ -0,0 +1,23 @@ +# 鸡尾酒排序 + +_Read this in other languages:_ +[_English_](README.md) + +鸡尾酒排序是一种双向气泡排序的算法。 + +该算法通过在两个方向上操作扩展了冒泡排序。虽然它通过更快速地将项目移到列表的开头而改进了冒泡排序,但它只提供了微弱的性能改进。 + +![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif) + + +## 复杂度 + +| 名称 | 最优 | 平均 | 最坏 | 内存 | 稳定 | 备注 | +| ------------------------------ | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | +| **鸡尾酒排序** | n | n2 | n2 | 1 | 是 | | + +## 参考 + +- [鸡尾酒排序(维基百科)](https://zh.wikipedia.org/wiki/%E9%B8%A1%E5%B0%BE%E9%85%92%E6%8E%92%E5%BA%8F) + +- [冒泡排序(维基百科)](https://zh.wikipedia.org/wiki/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F) \ No newline at end of file diff --git a/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js b/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js new file mode 100644 index 00000000..17cdea5e --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js @@ -0,0 +1,64 @@ +import CocktailShakerSort from '../CocktailShakerSort'; +import { + equalArr, + notSortedArr, + reverseArr, + sortedArr, + SortTester, +} from '../../SortTester'; + +// Complexity constants. +const SORTED_ARRAY_VISITING_COUNT = 19; +const NOT_SORTED_ARRAY_VISITING_COUNT = 159; +const REVERSE_SORTED_ARRAY_VISITING_COUNT = 200; +const EQUAL_ARRAY_VISITING_COUNT = 19; + +describe('CocktailShakerSort', () => { + it('should sort array', () => { + SortTester.testSort(CocktailShakerSort); + }); + + it('should sort array with custom comparator', () => { + SortTester.testSortWithCustomComparator(CocktailShakerSort); + }); + + it('should do stable sorting', () => { + SortTester.testSortStability(CocktailShakerSort); + }); + + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(CocktailShakerSort); + }); + + it('should visit EQUAL array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + equalArr, + EQUAL_ARRAY_VISITING_COUNT, + ); + }); + + it('should visit SORTED array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + sortedArr, + SORTED_ARRAY_VISITING_COUNT, + ); + }); + + it('should visit NOT SORTED array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + notSortedArr, + NOT_SORTED_ARRAY_VISITING_COUNT, + ); + }); + + it('should visit REVERSE SORTED array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + reverseArr, + REVERSE_SORTED_ARRAY_VISITING_COUNT, + ); + }); +}); From 4a6cb665deca1c8c1d68f3509a087835abb16fea Mon Sep 17 00:00:00 2001 From: Chong Yao Jun <82208147+Im-Not-God@users.noreply.github.com> Date: Mon, 29 Aug 2022 16:15:49 +0800 Subject: [PATCH 2/7] Update CocktailShakerSort.js --- .../sorting/cocktail-shaker-sort/CocktailShakerSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js index a0eb57eb..64b3f372 100644 --- a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js +++ b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js @@ -29,7 +29,7 @@ export default class CocktailShakerSort extends Sort { end --; // if nothing swapped, then array is sorted. - if (swapped == false) + if (swapped === false) break; // reset the swapped flag so that it can be used in the next stage From 7bdc35e099e869690e61536d98a059ffe7184b7d Mon Sep 17 00:00:00 2001 From: Chong Yao Jun <82208147+Im-Not-God@users.noreply.github.com> Date: Wed, 31 Aug 2022 00:05:18 +0800 Subject: [PATCH 3/7] Update CocktailShakerSort.js --- .../CocktailShakerSort.js | 30 +++++++++++-------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js index 64b3f372..f5755a2f 100644 --- a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js +++ b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js @@ -11,11 +11,12 @@ export default class CocktailShakerSort extends Sort { let end = array.length; while (swapped) { - // reset the swapped flag on entering the loop, because it might be true from a previous iteration. + // Reset the swapped flag on entering the loop, + // because it might be true from a previous iteration. swapped = false; - // loop forward same as the bubble sort - for (let i = start; i < end - 1; i++) { + // Loop forward same as the bubble sort + for (let i = start; i < end - 1; i += 1) { // Call visiting callback. this.callbacks.visitingCallback(array[i]); @@ -25,18 +26,20 @@ export default class CocktailShakerSort extends Sort { } } - // move the end point backward by one, because the item at the end point is already in its correct position. - end --; + // Move the end point backward by one, + // because the item at the end point is already in its correct position. + end -= 1; - // if nothing swapped, then array is sorted. - if (swapped === false) + // If nothing swapped, then array is sorted. + if (swapped === false) { break; - - // reset the swapped flag so that it can be used in the next stage + } + + // Reset the swapped flag so that it can be used in the next stage swapped = false; - // loop backward, doing the same comparison as in the previous stage - for (let i = end - 1; i >= start; i--) { + // Loop backward, doing the same comparison as in the previous stage + for (let i = end - 1; i >= start; i -= 1) { // Call visiting callback. this.callbacks.visitingCallback(array[i]); @@ -46,8 +49,9 @@ export default class CocktailShakerSort extends Sort { } } - // move the start point forward by one, because the item at the start point is already in its correct position. - start ++; + // Move the start point forward by one, + // because the item at the start point is already in its correct position. + start += 1; } return array; From b7bacf8870cbd6958586280bcb3d4dbfa8ce30bb Mon Sep 17 00:00:00 2001 From: Chong Yao Jun <82208147+Im-Not-God@users.noreply.github.com> Date: Fri, 2 Sep 2022 11:08:49 +0800 Subject: [PATCH 4/7] Delete src/algorithms/sorting/cocktail-shaker-sort directory --- .../CocktailShakerSort.js | 59 ----------------- .../sorting/cocktail-shaker-sort/README.md | 23 ------- .../cocktail-shaker-sort/README.zh-CN.md | 23 ------- .../__test__/CocktailShakerSort.test.js | 64 ------------------- 4 files changed, 169 deletions(-) delete mode 100644 src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js delete mode 100644 src/algorithms/sorting/cocktail-shaker-sort/README.md delete mode 100644 src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md delete mode 100644 src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js diff --git a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js deleted file mode 100644 index f5755a2f..00000000 --- a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js +++ /dev/null @@ -1,59 +0,0 @@ -import Sort from '../Sort'; - -export default class CocktailShakerSort extends Sort { - sort(originalArray) { - // Flag that holds info about whether the swap has occur or not. - let swapped = true; - // Clone original array to prevent its modification. - const array = [...originalArray]; - - let start = 0; - let end = array.length; - - while (swapped) { - // Reset the swapped flag on entering the loop, - // because it might be true from a previous iteration. - swapped = false; - - // Loop forward same as the bubble sort - for (let i = start; i < end - 1; i += 1) { - // Call visiting callback. - this.callbacks.visitingCallback(array[i]); - - if (this.comparator.greaterThan(array[i], array[i + 1])) { - [array[i], array[i + 1]] = [array[i + 1], array[i]]; - swapped = true; - } - } - - // Move the end point backward by one, - // because the item at the end point is already in its correct position. - end -= 1; - - // If nothing swapped, then array is sorted. - if (swapped === false) { - break; - } - - // Reset the swapped flag so that it can be used in the next stage - swapped = false; - - // Loop backward, doing the same comparison as in the previous stage - for (let i = end - 1; i >= start; i -= 1) { - // Call visiting callback. - this.callbacks.visitingCallback(array[i]); - - if (this.comparator.greaterThan(array[i], array[i + 1])) { - [array[i], array[i + 1]] = [array[i + 1], array[i]]; - swapped = true; - } - } - - // Move the start point forward by one, - // because the item at the start point is already in its correct position. - start += 1; - - } - return array; - } -} diff --git a/src/algorithms/sorting/cocktail-shaker-sort/README.md b/src/algorithms/sorting/cocktail-shaker-sort/README.md deleted file mode 100644 index 11769e26..00000000 --- a/src/algorithms/sorting/cocktail-shaker-sort/README.md +++ /dev/null @@ -1,23 +0,0 @@ -# Cocktail Shaker Sort - -_Read this in other languages:_ -[_简体中文_](README.zh-CN.md) - -Cocktail Shaker Sort is an algorithm that is a Bidirectional Bubble Sort. - -The algorithm extends bubble sort by operating in two directions. -While it improves on bubble sort by more quickly moving items to the beginning of the list, it provides only marginal performance improvements. - -![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif) - -## Complexity - -| Name | Best | Average | Worst | Memory | Stable | Comments | -| ------------------------------ | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | -| **Cocktail Shaker Sort** | n | n2 | n2 | 1 | Yes | | - -## References - -- [Cocktail Shaker Sort on Wikipedia](https://en.wikipedia.org/wiki/Cocktail_shaker_sort) - -- [Bubble Sort on Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort) diff --git a/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md b/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md deleted file mode 100644 index 2101a911..00000000 --- a/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md +++ /dev/null @@ -1,23 +0,0 @@ -# 鸡尾酒排序 - -_Read this in other languages:_ -[_English_](README.md) - -鸡尾酒排序是一种双向气泡排序的算法。 - -该算法通过在两个方向上操作扩展了冒泡排序。虽然它通过更快速地将项目移到列表的开头而改进了冒泡排序,但它只提供了微弱的性能改进。 - -![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif) - - -## 复杂度 - -| 名称 | 最优 | 平均 | 最坏 | 内存 | 稳定 | 备注 | -| ------------------------------ | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | -| **鸡尾酒排序** | n | n2 | n2 | 1 | 是 | | - -## 参考 - -- [鸡尾酒排序(维基百科)](https://zh.wikipedia.org/wiki/%E9%B8%A1%E5%B0%BE%E9%85%92%E6%8E%92%E5%BA%8F) - -- [冒泡排序(维基百科)](https://zh.wikipedia.org/wiki/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F) \ No newline at end of file diff --git a/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js b/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js deleted file mode 100644 index 17cdea5e..00000000 --- a/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js +++ /dev/null @@ -1,64 +0,0 @@ -import CocktailShakerSort from '../CocktailShakerSort'; -import { - equalArr, - notSortedArr, - reverseArr, - sortedArr, - SortTester, -} from '../../SortTester'; - -// Complexity constants. -const SORTED_ARRAY_VISITING_COUNT = 19; -const NOT_SORTED_ARRAY_VISITING_COUNT = 159; -const REVERSE_SORTED_ARRAY_VISITING_COUNT = 200; -const EQUAL_ARRAY_VISITING_COUNT = 19; - -describe('CocktailShakerSort', () => { - it('should sort array', () => { - SortTester.testSort(CocktailShakerSort); - }); - - it('should sort array with custom comparator', () => { - SortTester.testSortWithCustomComparator(CocktailShakerSort); - }); - - it('should do stable sorting', () => { - SortTester.testSortStability(CocktailShakerSort); - }); - - it('should sort negative numbers', () => { - SortTester.testNegativeNumbersSort(CocktailShakerSort); - }); - - it('should visit EQUAL array element specified number of times', () => { - SortTester.testAlgorithmTimeComplexity( - CocktailShakerSort, - equalArr, - EQUAL_ARRAY_VISITING_COUNT, - ); - }); - - it('should visit SORTED array element specified number of times', () => { - SortTester.testAlgorithmTimeComplexity( - CocktailShakerSort, - sortedArr, - SORTED_ARRAY_VISITING_COUNT, - ); - }); - - it('should visit NOT SORTED array element specified number of times', () => { - SortTester.testAlgorithmTimeComplexity( - CocktailShakerSort, - notSortedArr, - NOT_SORTED_ARRAY_VISITING_COUNT, - ); - }); - - it('should visit REVERSE SORTED array element specified number of times', () => { - SortTester.testAlgorithmTimeComplexity( - CocktailShakerSort, - reverseArr, - REVERSE_SORTED_ARRAY_VISITING_COUNT, - ); - }); -}); From 508e4d1313df575466c98f113b7becdf3e4130f6 Mon Sep 17 00:00:00 2001 From: Chong Yao Jun <82208147+Im-Not-God@users.noreply.github.com> Date: Fri, 2 Sep 2022 11:09:14 +0800 Subject: [PATCH 5/7] Add files via upload --- .../CocktailShakerSort.js | 59 +++++++++++++++++ .../sorting/cocktail-shaker-sort/README.md | 23 +++++++ .../cocktail-shaker-sort/README.zh-CN.md | 23 +++++++ .../__test__/CocktailShakerSort.test.js | 64 +++++++++++++++++++ 4 files changed, 169 insertions(+) create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/README.md create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md create mode 100644 src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js diff --git a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js new file mode 100644 index 00000000..f5755a2f --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js @@ -0,0 +1,59 @@ +import Sort from '../Sort'; + +export default class CocktailShakerSort extends Sort { + sort(originalArray) { + // Flag that holds info about whether the swap has occur or not. + let swapped = true; + // Clone original array to prevent its modification. + const array = [...originalArray]; + + let start = 0; + let end = array.length; + + while (swapped) { + // Reset the swapped flag on entering the loop, + // because it might be true from a previous iteration. + swapped = false; + + // Loop forward same as the bubble sort + for (let i = start; i < end - 1; i += 1) { + // Call visiting callback. + this.callbacks.visitingCallback(array[i]); + + if (this.comparator.greaterThan(array[i], array[i + 1])) { + [array[i], array[i + 1]] = [array[i + 1], array[i]]; + swapped = true; + } + } + + // Move the end point backward by one, + // because the item at the end point is already in its correct position. + end -= 1; + + // If nothing swapped, then array is sorted. + if (swapped === false) { + break; + } + + // Reset the swapped flag so that it can be used in the next stage + swapped = false; + + // Loop backward, doing the same comparison as in the previous stage + for (let i = end - 1; i >= start; i -= 1) { + // Call visiting callback. + this.callbacks.visitingCallback(array[i]); + + if (this.comparator.greaterThan(array[i], array[i + 1])) { + [array[i], array[i + 1]] = [array[i + 1], array[i]]; + swapped = true; + } + } + + // Move the start point forward by one, + // because the item at the start point is already in its correct position. + start += 1; + + } + return array; + } +} diff --git a/src/algorithms/sorting/cocktail-shaker-sort/README.md b/src/algorithms/sorting/cocktail-shaker-sort/README.md new file mode 100644 index 00000000..11769e26 --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/README.md @@ -0,0 +1,23 @@ +# Cocktail Shaker Sort + +_Read this in other languages:_ +[_简体中文_](README.zh-CN.md) + +Cocktail Shaker Sort is an algorithm that is a Bidirectional Bubble Sort. + +The algorithm extends bubble sort by operating in two directions. +While it improves on bubble sort by more quickly moving items to the beginning of the list, it provides only marginal performance improvements. + +![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif) + +## Complexity + +| Name | Best | Average | Worst | Memory | Stable | Comments | +| ------------------------------ | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | +| **Cocktail Shaker Sort** | n | n2 | n2 | 1 | Yes | | + +## References + +- [Cocktail Shaker Sort on Wikipedia](https://en.wikipedia.org/wiki/Cocktail_shaker_sort) + +- [Bubble Sort on Wikipedia](https://en.wikipedia.org/wiki/Bubble_sort) diff --git a/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md b/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md new file mode 100644 index 00000000..2101a911 --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/README.zh-CN.md @@ -0,0 +1,23 @@ +# 鸡尾酒排序 + +_Read this in other languages:_ +[_English_](README.md) + +鸡尾酒排序是一种双向气泡排序的算法。 + +该算法通过在两个方向上操作扩展了冒泡排序。虽然它通过更快速地将项目移到列表的开头而改进了冒泡排序,但它只提供了微弱的性能改进。 + +![Algorithm Visualization](https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif) + + +## 复杂度 + +| 名称 | 最优 | 平均 | 最坏 | 内存 | 稳定 | 备注 | +| ------------------------------ | :-------------: | :-----------------: | :-----------------: | :-------: | :-------: | :-------- | +| **鸡尾酒排序** | n | n2 | n2 | 1 | 是 | | + +## 参考 + +- [鸡尾酒排序(维基百科)](https://zh.wikipedia.org/wiki/%E9%B8%A1%E5%B0%BE%E9%85%92%E6%8E%92%E5%BA%8F) + +- [冒泡排序(维基百科)](https://zh.wikipedia.org/wiki/%E5%86%92%E6%B3%A1%E6%8E%92%E5%BA%8F) \ No newline at end of file diff --git a/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js b/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js new file mode 100644 index 00000000..17cdea5e --- /dev/null +++ b/src/algorithms/sorting/cocktail-shaker-sort/__test__/CocktailShakerSort.test.js @@ -0,0 +1,64 @@ +import CocktailShakerSort from '../CocktailShakerSort'; +import { + equalArr, + notSortedArr, + reverseArr, + sortedArr, + SortTester, +} from '../../SortTester'; + +// Complexity constants. +const SORTED_ARRAY_VISITING_COUNT = 19; +const NOT_SORTED_ARRAY_VISITING_COUNT = 159; +const REVERSE_SORTED_ARRAY_VISITING_COUNT = 200; +const EQUAL_ARRAY_VISITING_COUNT = 19; + +describe('CocktailShakerSort', () => { + it('should sort array', () => { + SortTester.testSort(CocktailShakerSort); + }); + + it('should sort array with custom comparator', () => { + SortTester.testSortWithCustomComparator(CocktailShakerSort); + }); + + it('should do stable sorting', () => { + SortTester.testSortStability(CocktailShakerSort); + }); + + it('should sort negative numbers', () => { + SortTester.testNegativeNumbersSort(CocktailShakerSort); + }); + + it('should visit EQUAL array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + equalArr, + EQUAL_ARRAY_VISITING_COUNT, + ); + }); + + it('should visit SORTED array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + sortedArr, + SORTED_ARRAY_VISITING_COUNT, + ); + }); + + it('should visit NOT SORTED array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + notSortedArr, + NOT_SORTED_ARRAY_VISITING_COUNT, + ); + }); + + it('should visit REVERSE SORTED array element specified number of times', () => { + SortTester.testAlgorithmTimeComplexity( + CocktailShakerSort, + reverseArr, + REVERSE_SORTED_ARRAY_VISITING_COUNT, + ); + }); +}); From 9c9f694caa06cd4c798e8a9376b49caa5eb6fa72 Mon Sep 17 00:00:00 2001 From: Chong Yao Jun <82208147+Im-Not-God@users.noreply.github.com> Date: Fri, 2 Sep 2022 11:13:52 +0800 Subject: [PATCH 6/7] Update CocktailShakerSort.js --- .../sorting/cocktail-shaker-sort/CocktailShakerSort.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js index f5755a2f..701d75a4 100644 --- a/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js +++ b/src/algorithms/sorting/cocktail-shaker-sort/CocktailShakerSort.js @@ -48,11 +48,10 @@ export default class CocktailShakerSort extends Sort { swapped = true; } } - // Move the start point forward by one, // because the item at the start point is already in its correct position. start += 1; - + } return array; } From 89e4c79845007348ba58ba08e09fc1581c30cd8b Mon Sep 17 00:00:00 2001 From: Chong Yao Jun <82208147+Im-Not-God@users.noreply.github.com> Date: Sat, 3 Sep 2022 18:27:25 +0800 Subject: [PATCH 7/7] Update CI.yml --- .github/workflows/CI.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 912c719d..a89e9ad2 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -23,10 +23,7 @@ jobs: node-version: ${{ matrix.node-version }} - name: Install dependencies - run: npm i - - - name: Run linting - run: npm run lint + run: npm ci - name: Run tests run: npm run coverage