Skip to content

Instantly share code, notes, and snippets.

@james-prado
Last active January 25, 2017 18:47
Show Gist options
  • Select an option

  • Save james-prado/4fd222d65f93aa6fc62c452942c3b5b7 to your computer and use it in GitHub Desktop.

Select an option

Save james-prado/4fd222d65f93aa6fc62c452942c3b5b7 to your computer and use it in GitHub Desktop.

Revisions

  1. james-prado revised this gist Jan 25, 2017. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions quicksort.js
    Original file line number Diff line number Diff line change
    @@ -9,8 +9,7 @@ function quickSort(numbers, target, partial) {

    // check if the sum partial equals the target
    if (sum === target) {
    console.log("%s=%s", partial.join("+"), target);
    console.log('Sum: ' + sum + ', Target: ' + target + ', Partial: ' + partial);
    console.log("%s=%s", partial.join("+"), target + ', Sum: ' + sum + ', Target: ' + target + ', Partial: ' + partial + ', Remaining: ' + numbers);
    return;
    } else if (sum > target) {
    return // if we're past the target, there is no reason to continue
  2. james-prado revised this gist Jan 25, 2017. 1 changed file with 3 additions and 2 deletions.
    5 changes: 3 additions & 2 deletions quicksort.js
    Original file line number Diff line number Diff line change
    @@ -10,11 +10,10 @@ function quickSort(numbers, target, partial) {
    // check if the sum partial equals the target
    if (sum === target) {
    console.log("%s=%s", partial.join("+"), target);
    console.log('Sum: ' + sum + ', Target: ' + target + ', Partial: ' + partial);
    return;
    } else if (sum > target) {
    return // if we're past the target, there is no reason to continue
    } else {
    console.log('Sum: ' + sum + ', Target: ' + target + ', Partial: ' + partial);
    }

    for (var i = 0; i < numbers.length; i++) {
    @@ -24,4 +23,6 @@ function quickSort(numbers, target, partial) {
    }
    }

    // Examples
    quickSort([100, 50, 20, 10, 5, 1], 176);
    quickSort([3, 9, 8, 4, 5, 7, 10], 15);
  3. james-prado revised this gist Jan 25, 2017. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions quicksort.js
    Original file line number Diff line number Diff line change
    @@ -7,14 +7,14 @@ function quickSort(numbers, target, partial) {
    return a + b;
    }, 0);

    // check if the partial sum is equals to target
    // check if the sum partial equals the target
    if (sum === target) {
    console.log("%s=%s", partial.join("+"), target);
    return;
    } else if (sum > target) {
    return // if we reach the number why bother to continue
    return // if we're past the target, there is no reason to continue
    } else {
    console.log('Sum: ' + sum + ', Target: ' + target);
    console.log('Sum: ' + sum + ', Target: ' + target + ', Partial: ' + partial);
    }

    for (var i = 0; i < numbers.length; i++) {
  4. james-prado created this gist Jan 25, 2017.
    27 changes: 27 additions & 0 deletions quicksort.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    function quickSort(numbers, target, partial) {
    var sum, remaining;
    var partial = partial || [];

    // sum partial
    sum = partial.reduce(function (a, b) {
    return a + b;
    }, 0);

    // check if the partial sum is equals to target
    if (sum === target) {
    console.log("%s=%s", partial.join("+"), target);
    return;
    } else if (sum > target) {
    return // if we reach the number why bother to continue
    } else {
    console.log('Sum: ' + sum + ', Target: ' + target);
    }

    for (var i = 0; i < numbers.length; i++) {
    number = numbers[i];
    remaining = numbers.slice(i + 1);
    quickSort(remaining, target, partial.concat([number]));
    }
    }

    quickSort([100, 50, 20, 10, 5, 1], 176);