A little one version in pretty damn cryptic but possibly speedy JS.
const waveSort = array => array .sort((a, b) => b - a) .map((_, i, arr) => arr[(i & 1) * ((arr.length + 1) >> 1) + (i >> 1)]);
The call to sort sorts the array by descending value.
sort
The call to map performs a one-to-one mapping of elements:
map
(i & 1) * ((arr.length + 1) >> 1)
Because the elements in the first half are all greater than or equal to the elements in the first half, it's a wave sort.
Tested in the kata
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
A little one version in pretty damn cryptic but possibly speedy JS.
The call to
sort
sorts the array by descending value.The call to
map
performs a one-to-one mapping of elements:(i & 1) * ((arr.length + 1) >> 1)
alternates between the first half of the array and the second halfBecause the elements in the first half are all greater than or equal to the elements in the first half, it's a wave sort.
Tested in the kata