FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScript/Sorts/SimplifiedWiggleSort.js at master · TheAlgorithms/JavaScript · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
TheAlgorithms
/
JavaScript
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
5.8k
Star
34.2k
Code
Issues
21
Pull requests
192
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScript
/
Sorts
/
SimplifiedWiggleSort.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
36 lines (31 loc) · 1.19 KB
Breadcrumbs
JavaScript
/
Sorts
/
SimplifiedWiggleSort.js
Copy path
File metadata and controls
36 lines (31 loc) · 1.19 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
28
29
30
31
32
33
34
35
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
}
Back
|
FazBrowse Home
|
New Git URL