Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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
appgurueu merged 7 commits into TheAlgorithms:master from BranAndSceolan:fixWiggleSort
Apr 28, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Sorts/SimplifiedWiggleSort.js
View file Open in desktop
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
}
21 changes: 0 additions & 21 deletions Sorts/WiggleSort.js
View file Open in desktop

This file was deleted.

24 changes: 24 additions & 0 deletions Sorts/test/SimplifiedWiggleSort.test.js
View file Open in desktop
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])
})
})

AltStyle によって変換されたページ (->オリジナル) /