From 2a09635db606b6b82d59ad8ad6a0fdd814b42881 Mon Sep 17 00:00:00 2001 From: Madiena Date: Wed, 27 Apr 2022 16:35:43 +0200 Subject: [PATCH 1/4] Add stooge sort sorting algorithm with included tests --- Sorts/StoogeSort.js | 0 Sorts/test/StoogeSort.test.js | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 Sorts/StoogeSort.js create mode 100644 Sorts/test/StoogeSort.test.js diff --git a/Sorts/StoogeSort.js b/Sorts/StoogeSort.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/Sorts/test/StoogeSort.test.js b/Sorts/test/StoogeSort.test.js new file mode 100644 index 0000000000..e69de29bb2 From a456b3043f3882c95bd6945b5a8a9fa03e764a67 Mon Sep 17 00:00:00 2001 From: Madiena Date: Wed, 27 Apr 2022 17:11:12 +0200 Subject: [PATCH 2/4] Add stooge sort sorting algorithm with included tests --- Sorts/StoogeSort.js | 21 +++++++++++++++++++++ Sorts/test/StoogeSort.test.js | 31 +++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/Sorts/StoogeSort.js b/Sorts/StoogeSort.js index e69de29bb2..6fd72d2ad7 100644 --- a/Sorts/StoogeSort.js +++ b/Sorts/StoogeSort.js @@ -0,0 +1,21 @@ +/* + * Stooge Sort sorts an array based on divide and conquer principle + * more information: https://en.wikipedia.org/wiki/Shellsort + * + */ +export function stoogeSort (items, leftEnd, rightEnd) { + if (items[rightEnd - 1] < items[leftEnd]) { + const temp = items[leftEnd] + items[leftEnd] = items[rightEnd - 1] + items[rightEnd - 1] = temp + } + const length = rightEnd - leftEnd + if (length > 2) { + const third = Math.floor(length / 3) + stoogeSort(items, leftEnd, rightEnd - third) + stoogeSort(items, leftEnd + third, rightEnd) + stoogeSort(items, leftEnd, rightEnd - third) + } + console.log(items) + return items +} diff --git a/Sorts/test/StoogeSort.test.js b/Sorts/test/StoogeSort.test.js index e69de29bb2..bd3250c78b 100644 --- a/Sorts/test/StoogeSort.test.js +++ b/Sorts/test/StoogeSort.test.js @@ -0,0 +1,31 @@ +import { stoogeSort } from '../StoogeSort' + +test('The StoogeSort of the array [1, 6, 4, 7, 2] is [1, 2, 4, 6, 7]', () => { + const arr = [1, 6, 4, 7, 2] + const res = stoogeSort(arr, 0, arr.length) + expect(res).toEqual([1, 2, 4, 6, 7]) +}) + +test('The StoogeSort of the array [] is []', () => { + const arr = [] + const res = stoogeSort(arr, 0, arr.length) + expect(res).toEqual([]) +}) + +test('The StoogeSort of the array [46, 15, 49, 65, 23] is [15, 23, 46, 49, 65]', () => { + const arr = [46, 15, 49, 65, 23] + const res = stoogeSort(arr, 0, arr.length) + expect(res).toEqual([15, 23, 46, 49, 65]) +}) + +test('The StoogeSort of the array [136, 459, 132, 566, 465] is [132, 136, 459, 465, 566]', () => { + const arr = [136, 459, 132, 566, 465] + const res = stoogeSort(arr, 0, arr.length) + expect(res).toEqual([132, 136, 459, 465, 566]) +}) + +test('The StoogeSort of the array [45, 3, 156, 1, 56] is [1, 3, 45, 56, 156]', () => { + const arr = [45, 3, 156, 1, 56] + const res = stoogeSort(arr, 0, arr.length) + expect(res).toEqual([1, 3, 45, 56, 156]) +}) From 43ec87f36f710fe993b2bf24792d8680a7b9ee18 Mon Sep 17 00:00:00 2001 From: Madiena Date: Wed, 27 Apr 2022 17:19:38 +0200 Subject: [PATCH 3/4] Add correct url for more information --- Sorts/StoogeSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/StoogeSort.js b/Sorts/StoogeSort.js index 6fd72d2ad7..0ff81e2943 100644 --- a/Sorts/StoogeSort.js +++ b/Sorts/StoogeSort.js @@ -1,6 +1,6 @@ /* * Stooge Sort sorts an array based on divide and conquer principle - * more information: https://en.wikipedia.org/wiki/Shellsort + * more information: https://en.wikipedia.org/wiki/Stooge_sort * */ export function stoogeSort (items, leftEnd, rightEnd) { From e7a05ac20c60cecf84a0b528aba4762004f60632 Mon Sep 17 00:00:00 2001 From: Madiena Date: Wed, 27 Apr 2022 18:08:04 +0200 Subject: [PATCH 4/4] Add time complexity warning --- Sorts/StoogeSort.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/StoogeSort.js b/Sorts/StoogeSort.js index 0ff81e2943..5b1b6bdefc 100644 --- a/Sorts/StoogeSort.js +++ b/Sorts/StoogeSort.js @@ -1,5 +1,6 @@ /* * Stooge Sort sorts an array based on divide and conquer principle + * note the exceptionally bad time complexity * more information: https://en.wikipedia.org/wiki/Stooge_sort * */ @@ -16,6 +17,5 @@ export function stoogeSort (items, leftEnd, rightEnd) { stoogeSort(items, leftEnd + third, rightEnd) stoogeSort(items, leftEnd, rightEnd - third) } - console.log(items) return items }