forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWiggleSort.js
More file actions
27 lines (23 loc) · 757 Bytes
/
WiggleSort.js
File metadata and controls
27 lines (23 loc) · 757 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*
* 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] >= …..
*
*/
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
Array.prototype.wiggleSort = function () {
for (let i = 0; i < this.length; ++i) {
const shouldNotBeLessThan = i % 2
const isLessThan = this[i] < this[i + 1]
if (shouldNotBeLessThan && isLessThan) {
[this[i], this[i + 1]] = [this[i + 1], this[i]]
}
}
return this
}
// Implementation of wiggle sort
var arr = [3, 5, 2, 1, 6, 4]
// Array before Wiggle Sort
console.log(arr) // [3, 5, 2, 1, 6, 4]
arr.wiggleSort()
// Array after wiggle sort
console.log(arr) // [ 3, 5, 2, 6, 1, 4 ]