From 6d3226889afdde75e17715faf09ed2e7f0cad16b Mon Sep 17 00:00:00 2001 From: Antonia Strack Date: Thu, 21 Apr 2022 09:57:56 +0200 Subject: [PATCH 1/7] Add simple wiggle-sort --- Sorts/WiggleSort.js | 45 ++++++++++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 9 deletions(-) diff --git a/Sorts/WiggleSort.js b/Sorts/WiggleSort.js index aa19d596e6..954607e7bd 100644 --- a/Sorts/WiggleSort.js +++ b/Sorts/WiggleSort.js @@ -1,21 +1,48 @@ /* * Wiggle sort sorts the array into a wave like array. - * An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] >= arr[1] <= arr[2] >= arr[3] <= arr[4] >= ….. - * + * An array ‘arr[0..n-1]’ is sorted in wave form if arr[0] <= arr[1] >= arr[2] <= arr[3] >= arr[4] <= ….. + * KEEP IN MIND: there are also more strict definitions of wiggle sort which use + * the rule arr[0] < arr[1] > arr[2] < arr[3] > arr[4] < … but this function + * allows for equality of values next to each other. */ +import { quickSelectSearch } from '../Search/QuickSelectSearch.js' export const wiggleSort = function (arr) { - for (let i = 0; i < arr.length; ++i) { - const shouldNotBeLessThan = i % 2 - const isLessThan = arr[i] < arr[i + 1] - if (shouldNotBeLessThan && isLessThan) { - [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]] + // find Median using QuickSelect + let median = quickSelectSearch(arr, Math.floor(arr.length / 2.0)) + median = median[Math.floor(arr.length / 2.0)] + + console.log(median) + const sorted = new Array(arr.length) + + let smallerThanMedianIndx = 0 + let greaterThanMedianIndx = arr.length - 1 - (arr.length % 2) + + for (let i = 0; i < arr.length; i++) { + console.log(sorted) + if (arr[i] > median) { + sorted[greaterThanMedianIndx] = arr[i] + greaterThanMedianIndx -= 2 + } else { + if (smallerThanMedianIndx < arr.length) { + sorted[smallerThanMedianIndx] = arr[i] + smallerThanMedianIndx += 2 + } else { + sorted[greaterThanMedianIndx] = arr[i] + greaterThanMedianIndx -= 2 + } } } - return arr + + return sorted } // Implementation of wiggle sort -// > wiggleSort([3, 5, 2, 1, 6, 4]) +console.log(wiggleSort([3, 5, 2, 1, 6, 4])) // [ 3, 5, 2, 6, 1, 4 ] +console.log(wiggleSort([3, 5, 2, 2, 0, 2])) +// [ 0, 5, 2, 3, 2, 2 ] +console.log(wiggleSort([1, 1, 1, 2, 2])) +// [ 1, 2, 1, 2, 1 ] +console.log(wiggleSort([1, 1, 2, 2, 2])) From 4490ed00c8a963cd68a2565f9f05e8e5a6351402 Mon Sep 17 00:00:00 2001 From: Antonia Strack Date: Thu, 21 Apr 2022 10:02:27 +0200 Subject: [PATCH 2/7] Add comments with demonstration of behaviour. --- Sorts/WiggleSort.js | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/Sorts/WiggleSort.js b/Sorts/WiggleSort.js index 954607e7bd..fd7af585cc 100644 --- a/Sorts/WiggleSort.js +++ b/Sorts/WiggleSort.js @@ -39,10 +39,13 @@ export const wiggleSort = function (arr) { // Implementation of wiggle sort -console.log(wiggleSort([3, 5, 2, 1, 6, 4])) +// console.log(wiggleSort([3, 5, 2, 1, 6, 4])) // [ 3, 5, 2, 6, 1, 4 ] -console.log(wiggleSort([3, 5, 2, 2, 0, 2])) +// console.log(wiggleSort([3, 5, 2, 2, 0, 2])) // [ 0, 5, 2, 3, 2, 2 ] -console.log(wiggleSort([1, 1, 1, 2, 2])) +// console.log(wiggleSort([1, 1, 1, 2, 2])) // [ 1, 2, 1, 2, 1 ] -console.log(wiggleSort([1, 1, 2, 2, 2])) +// console.log(wiggleSort([1, 1, 2, 2, 2])) +// [ 1, 2, 1, 2, 2 ] +// console.log(wiggleSort([3, 5, 6, 1])) +// [ 3, 6, 1, 5 ] From db8abd3333755b31e61d94ca676bff1ab3b23cfc Mon Sep 17 00:00:00 2001 From: Antonia Strack Date: Thu, 21 Apr 2022 10:08:26 +0200 Subject: [PATCH 3/7] Rename filename to be more fitting. Real wiggle-sort uses the rule arr[0] < arr[1] > arr[2] < arr[3] > arr[4] < ... --- Sorts/{WiggleSort.js => SimplifiedWiggleSort.js} | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) rename Sorts/{WiggleSort.js => SimplifiedWiggleSort.js} (80%) diff --git a/Sorts/WiggleSort.js b/Sorts/SimplifiedWiggleSort.js similarity index 80% rename from Sorts/WiggleSort.js rename to Sorts/SimplifiedWiggleSort.js index fd7af585cc..461a290ff6 100644 --- a/Sorts/WiggleSort.js +++ b/Sorts/SimplifiedWiggleSort.js @@ -7,7 +7,7 @@ */ import { quickSelectSearch } from '../Search/QuickSelectSearch.js' -export const wiggleSort = function (arr) { +export const simplifiedWiggleSort = function (arr) { // find Median using QuickSelect let median = quickSelectSearch(arr, Math.floor(arr.length / 2.0)) median = median[Math.floor(arr.length / 2.0)] @@ -39,13 +39,13 @@ export const wiggleSort = function (arr) { // Implementation of wiggle sort -// console.log(wiggleSort([3, 5, 2, 1, 6, 4])) +// console.log(simplifiedWiggleSort([3, 5, 2, 1, 6, 4])) // [ 3, 5, 2, 6, 1, 4 ] -// console.log(wiggleSort([3, 5, 2, 2, 0, 2])) +// console.log(simplifiedWiggleSort([3, 5, 2, 2, 0, 2])) // [ 0, 5, 2, 3, 2, 2 ] -// console.log(wiggleSort([1, 1, 1, 2, 2])) +// console.log(simplifiedWiggleSort([1, 1, 1, 2, 2])) // [ 1, 2, 1, 2, 1 ] -// console.log(wiggleSort([1, 1, 2, 2, 2])) +// console.log(simplifiedWiggleSort([1, 1, 2, 2, 2])) // [ 1, 2, 1, 2, 2 ] -// console.log(wiggleSort([3, 5, 6, 1])) +// console.log(simplifiedWiggleSort([3, 5, 6, 1])) // [ 3, 6, 1, 5 ] From b89bd2c2585a02f3c475d5de7f71699fac9f6f2c Mon Sep 17 00:00:00 2001 From: Antonia Strack Date: Thu, 21 Apr 2022 10:13:02 +0200 Subject: [PATCH 4/7] Remove console.log() --- Sorts/SimplifiedWiggleSort.js | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/Sorts/SimplifiedWiggleSort.js b/Sorts/SimplifiedWiggleSort.js index 461a290ff6..b3f85d6e83 100644 --- a/Sorts/SimplifiedWiggleSort.js +++ b/Sorts/SimplifiedWiggleSort.js @@ -12,14 +12,12 @@ export const simplifiedWiggleSort = function (arr) { let median = quickSelectSearch(arr, Math.floor(arr.length / 2.0)) median = median[Math.floor(arr.length / 2.0)] - console.log(median) const sorted = new Array(arr.length) let smallerThanMedianIndx = 0 let greaterThanMedianIndx = arr.length - 1 - (arr.length % 2) for (let i = 0; i < arr.length; i++) { - console.log(sorted) if (arr[i] > median) { sorted[greaterThanMedianIndx] = arr[i] greaterThanMedianIndx -= 2 @@ -39,13 +37,13 @@ export const simplifiedWiggleSort = function (arr) { // Implementation of wiggle sort -// console.log(simplifiedWiggleSort([3, 5, 2, 1, 6, 4])) +// simplifiedWiggleSort([3, 5, 2, 1, 6, 4]) // [ 3, 5, 2, 6, 1, 4 ] -// console.log(simplifiedWiggleSort([3, 5, 2, 2, 0, 2])) -// [ 0, 5, 2, 3, 2, 2 ] -// console.log(simplifiedWiggleSort([1, 1, 1, 2, 2])) +// simplifiedWiggleSort([3, 5, 2, 2, 0, 2]) +// [ 0, 5, 2, 3, 2, 2 ] +// simplifiedWiggleSort([1, 1, 1, 2, 2]) // [ 1, 2, 1, 2, 1 ] -// console.log(simplifiedWiggleSort([1, 1, 2, 2, 2])) +// simplifiedWiggleSort([1, 1, 2, 2, 2]) // [ 1, 2, 1, 2, 2 ] -// console.log(simplifiedWiggleSort([3, 5, 6, 1])) +// simplifiedWiggleSort([3, 5, 6, 1]) // [ 3, 6, 1, 5 ] From daef5fc103ba4dc6d002c30bbd6f4f301acc139a Mon Sep 17 00:00:00 2001 From: Antonia Strack Date: Thu, 21 Apr 2022 14:32:09 +0200 Subject: [PATCH 5/7] add tests, remove comments containing tests --- Sorts/SimplifiedWiggleSort.js | 13 ----------- Sorts/test/SimplifiedWiggleSort.test.js | 29 +++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 13 deletions(-) create mode 100644 Sorts/test/SimplifiedWiggleSort.test.js diff --git a/Sorts/SimplifiedWiggleSort.js b/Sorts/SimplifiedWiggleSort.js index b3f85d6e83..ae0ea54e20 100644 --- a/Sorts/SimplifiedWiggleSort.js +++ b/Sorts/SimplifiedWiggleSort.js @@ -34,16 +34,3 @@ export const simplifiedWiggleSort = function (arr) { return sorted } - -// Implementation of wiggle sort - -// simplifiedWiggleSort([3, 5, 2, 1, 6, 4]) -// [ 3, 5, 2, 6, 1, 4 ] -// simplifiedWiggleSort([3, 5, 2, 2, 0, 2]) -// [ 0, 5, 2, 3, 2, 2 ] -// simplifiedWiggleSort([1, 1, 1, 2, 2]) -// [ 1, 2, 1, 2, 1 ] -// simplifiedWiggleSort([1, 1, 2, 2, 2]) -// [ 1, 2, 1, 2, 2 ] -// simplifiedWiggleSort([3, 5, 6, 1]) -// [ 3, 6, 1, 5 ] diff --git a/Sorts/test/SimplifiedWiggleSort.test.js b/Sorts/test/SimplifiedWiggleSort.test.js new file mode 100644 index 0000000000..1db5db22af --- /dev/null +++ b/Sorts/test/SimplifiedWiggleSort.test.js @@ -0,0 +1,29 @@ +import { simplifiedWiggleSort } from '../SimplifiedWiggleSort.js' + +describe('simplified wiggle sort', () => { + test('given array of symbols [\'b\', \'a\', \'c\'] return wiggle sorted array [\'a\', \'c\', \'b\']', () => { + const src = ['a', 'b', 'c'] + expect(simplifiedWiggleSort(src)).toEqual(['a', 'c', 'b']) + }) + + test('wiggle sort with duplicates, even array', () => { + const src = [2, 2, 1, 3] + expect(simplifiedWiggleSort(src)).toEqual([1, 3, 2, 2]) + }) + + test('wiggle sort with duplicates, odd array', () => { + const src = [1, 1, 1, 2, 4] + expect(simplifiedWiggleSort(src)).toEqual([1, 4, 1, 2, 1]) + }) + + test('simplified wiggle sort for [3, 5, 6, 1]', () => { + const src = [3, 5, 6, 1] + expect(simplifiedWiggleSort(src)).toEqual([3, 6, 1, 5]) + }) + + test('simplified wiggle sort for [3, 3, 5, 1] (equal values next to ' + + 'each other can not be avoided in simplified version)', () => { + const src = [3, 3, 5, 1] + expect(simplifiedWiggleSort(src)).toEqual([1, 5, 3, 3]) + }) +}) From 6617a7a79c3f8fd634791d8f253bb941b756780b Mon Sep 17 00:00:00 2001 From: Antonia Strack Date: Thu, 21 Apr 2022 14:44:26 +0200 Subject: [PATCH 6/7] add tests, remove data from testname --- Sorts/test/SimplifiedWiggleSort.test.js | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/Sorts/test/SimplifiedWiggleSort.test.js b/Sorts/test/SimplifiedWiggleSort.test.js index 1db5db22af..09417611df 100644 --- a/Sorts/test/SimplifiedWiggleSort.test.js +++ b/Sorts/test/SimplifiedWiggleSort.test.js @@ -1,7 +1,7 @@ import { simplifiedWiggleSort } from '../SimplifiedWiggleSort.js' describe('simplified wiggle sort', () => { - test('given array of symbols [\'b\', \'a\', \'c\'] return wiggle sorted array [\'a\', \'c\', \'b\']', () => { + test('simplified wiggle sort for chars', () => { const src = ['a', 'b', 'c'] expect(simplifiedWiggleSort(src)).toEqual(['a', 'c', 'b']) }) @@ -16,13 +16,8 @@ describe('simplified wiggle sort', () => { expect(simplifiedWiggleSort(src)).toEqual([1, 4, 1, 2, 1]) }) - test('simplified wiggle sort for [3, 5, 6, 1]', () => { - const src = [3, 5, 6, 1] - expect(simplifiedWiggleSort(src)).toEqual([3, 6, 1, 5]) - }) - - test('simplified wiggle sort for [3, 3, 5, 1] (equal values next to ' + - 'each other can not be avoided in simplified version)', () => { + test('simplified wiggle sort wich leads to equal values next to ' + + 'each other', () => { const src = [3, 3, 5, 1] expect(simplifiedWiggleSort(src)).toEqual([1, 5, 3, 3]) }) From c8c874ca56fdcb0fe05d8579e5efeb13b5b23c0d Mon Sep 17 00:00:00 2001 From: BranAndSceolan <98578040+BranAndSceolan@users.noreply.github.com> Date: Mon, 25 Apr 2022 11:23:32 +0200 Subject: [PATCH 7/7] Update SimplifiedWiggleSort.test.js fix typo: wich => which --- Sorts/test/SimplifiedWiggleSort.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sorts/test/SimplifiedWiggleSort.test.js b/Sorts/test/SimplifiedWiggleSort.test.js index 09417611df..9727024010 100644 --- a/Sorts/test/SimplifiedWiggleSort.test.js +++ b/Sorts/test/SimplifiedWiggleSort.test.js @@ -16,7 +16,7 @@ describe('simplified wiggle sort', () => { expect(simplifiedWiggleSort(src)).toEqual([1, 4, 1, 2, 1]) }) - test('simplified wiggle sort wich leads to equal values next to ' + + test('simplified wiggle sort which leads to equal values next to ' + 'each other', () => { const src = [3, 3, 5, 1] expect(simplifiedWiggleSort(src)).toEqual([1, 5, 3, 3])