-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Fix wiggle sort #991
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Fix wiggle sort #991
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
6d32268
Add simple wiggle-sort
4490ed0
Add comments with demonstration of behaviour.
db8abd3
Rename filename to be more fitting.
b89bd2c
Remove console.log()
daef5fc
add tests, remove comments containing tests
6617a7a
add tests, remove data from testname
c8c874c
Update SimplifiedWiggleSort.test.js
BranAndSceolan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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]) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.