Skip to content

Instantly share code, notes, and snippets.

@jesuarva
Created July 12, 2018 15:26
Show Gist options
  • Select an option

  • Save jesuarva/40d3e3be4e3a65efeb923e05fbff7609 to your computer and use it in GitHub Desktop.

Select an option

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.
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