Created
July 12, 2018 15:26
-
-
Save jesuarva/40d3e3be4e3a65efeb923e05fbff7609 to your computer and use it in GitHub Desktop.
Given an array comprised of 0s and 1s, write a function in linear time that will sort the array.
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
| function binarySort(arr) { | |
| let control = ''; | |
| arr.forEach((digit, index, arr) => { | |
| !control && arr[index] == 1 && (control = index); | |
| if (control && arr[index] == 0) { | |
| arr.splice(index, 1); | |
| arr.splice(control, 0, 0); | |
| control++; | |
| } | |
| }); | |
| return arr; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment