diff --git a/Sorts/SimplifiedWiggleSort.js b/Sorts/SimplifiedWiggleSort.js new file mode 100644 index 0000000000..ae0ea54e20 --- /dev/null +++ b/Sorts/SimplifiedWiggleSort.js @@ -0,0 +1,36 @@ +/* + * 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] <= ….. + * 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 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)] + + 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++) { + 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 sorted +} diff --git a/Sorts/WiggleSort.js b/Sorts/WiggleSort.js deleted file mode 100644 index aa19d596e6..0000000000 --- a/Sorts/WiggleSort.js +++ /dev/null @@ -1,21 +0,0 @@ -/* - * 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] >= ….. - * - */ - -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]] - } - } - return arr -} - -// Implementation of wiggle sort - -// > wiggleSort([3, 5, 2, 1, 6, 4]) -// [ 3, 5, 2, 6, 1, 4 ] diff --git a/Sorts/test/SimplifiedWiggleSort.test.js b/Sorts/test/SimplifiedWiggleSort.test.js new file mode 100644 index 0000000000..9727024010 --- /dev/null +++ b/Sorts/test/SimplifiedWiggleSort.test.js @@ -0,0 +1,24 @@ +import { simplifiedWiggleSort } from '../SimplifiedWiggleSort.js' + +describe('simplified wiggle sort', () => { + test('simplified wiggle sort for chars', () => { + 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 which leads to equal values next to ' + + 'each other', () => { + const src = [3, 3, 5, 1] + expect(simplifiedWiggleSort(src)).toEqual([1, 5, 3, 3]) + }) +})