Skip to content

Instantly share code, notes, and snippets.

@KolosovAO
Created May 25, 2018 17:25
Show Gist options
  • Save KolosovAO/4066a343b1c5fcc5ba6d8c9e72cf064d to your computer and use it in GitHub Desktop.
Save KolosovAO/4066a343b1c5fcc5ba6d8c9e72cf064d to your computer and use it in GitHub Desktop.
function findPlace(x, arr) {
    const len = arr.length;
    if (!len || x > arr[0]) return 1;
    if (x <= arr[len - 1]) return len + 1;

    const pivot = midPivot(arr.length);
    // const pivot = randomPivot(arr.length);
 
    if (arr[pivot] < x) {
        return findPlace(x, arr.slice(0, pivot));
    } else {
        return pivot + findPlace(x, arr.slice(pivot));
    }
}

function midPivot(len) {
    return Math.round(len / 2);
}
function randomPivot(len) {
    return Math.floor(Math.random() * (len - 1)) + 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment