Skip to content

Hotfix/sorting #63

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions benchmarks/two-sum-implementations/01-two-sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Brute force: O(n^2) | O(1)
function twoSum(nums, target) {
for (let i = 0; i < nums.length - 1; i++) { // O(n^2)
for (let j = i + 1; j < nums.length; j++) { // O(n)
if (nums[i] + nums[j] === target) {
return [i, j];
}
}
}
return [];
}

module.exports = twoSum;
19 changes: 19 additions & 0 deletions benchmarks/two-sum-implementations/02-two-sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// With a HashMap: O(n) | O(n)
function twoSum(nums, target) {
const map = nums.reduce((m, v, i) => { // O(n)
const ids = m.get(v) || [];
ids.push(i);
return m.set(v, ids);
}, new Map());

for (let i = 0; i < nums.length; i++) { // O(n)
const diff = target - nums[i];
if (map.has(diff) && i !== map.get(diff)) {
return [i, map.get(diff)];
}
}

return [];
}

module.exports = twoSum;
18 changes: 18 additions & 0 deletions benchmarks/two-sum-implementations/03-two-sum.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// With a HashMap: O(n) | O(n), 1-pass
function twoSum(nums, target) {
const map = new Map();

for (let i = 0; i < nums.length; i++) { // O(n)
const complement = target - nums[i];

if (map.has(complement)) {
return [map.get(complement), i];
}

map.set(nums[i], i);
}

return [];
}

module.exports = twoSum;
16 changes: 16 additions & 0 deletions benchmarks/two-sum-implementations/runner.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const assert = require('assert');

const twoSum = require('./01-two-sum');

function test() {
assert.deepEqual(twoSum([1, 2, 3], 4), [0, 2]);
assert.deepEqual(twoSum([1, 2, 3], 14), []);

assert.deepEqual(twoSum([2, 2, 2], 4), [0, 1]);
assert.deepEqual(twoSum(Array(1e7).fill(2), 4), [0, 1]); //
// assert.deepEqual(twoSum(Array(1e8).fill(2), 4), [0, 1]); // FATAL ERROR: Ineffective mark-compacts near heap limit Allocation failed - JavaScript heap out of memory
// assert.deepEqual(twoSum(Array(1e9).fill(2), 4), [0, 1]); // 1e7 - error 137 - OUT OF MEMORY
console.log('All tests passed!');
}

test();
1 change: 1 addition & 0 deletions book/content/part04/bubble-sort.asc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ ifndef::imagesdir[]
:codedir: ../../../src
endif::[]

[[bubble-sort]]
==== Bubble Sort
(((Bubble Sort)))
(((Sorting, Bubble Sort)))
Expand Down
4 changes: 2 additions & 2 deletions book/content/part04/quick-sort.asc
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,11 @@ With the optimization, Quicksort has an _O(n log n)_ running time. Similar to th
- <<Online>>: [big]#️❌# No, the pivot element can be choose at random.
- Recursive: Yes
- Time Complexity: [big]#✅# <<part01-algorithms-analysis#linearithmic>> _O(n log n)_
- Space Complexity: [big]#✅# <<part01-algorithms-analysis#constant>> _O(1)_
- Space Complexity: [big]#✅# <<part01-algorithms-analysis#logarithmic>> _O(log n)_, because of recursion.

(((Linearithmic)))
(((Runtime, Linearithmic)))
(((Space complexity, Constant)))
(((Space complexity, Logarithmic)))

// Resources:
// https://www.khanacademy.org/computing/computer-science/algorithms/quick-sort/a/linear-time-partitioning
Expand Down
9 changes: 4 additions & 5 deletions book/content/part04/sorting-algorithms.asc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Sorting is one of the most common solutions when we want to extract some insight
We can sort to get the maximum or minimum value and many algorithmic problems involves sorting data first.

.We are going to explore three basic sorting algorithms _O(n^2^)_ which have low overhead:
- <<part04-algorithmic-toolbox#insertion-sort>>
- <<part04-algorithmic-toolbox#bubble-sort>>
- <<part04-algorithmic-toolbox#selection-sort>>
- <<part04-algorithmic-toolbox#insertion-sort>>

Expand Down Expand Up @@ -120,7 +120,7 @@ We explored many algorithms some of them simple and other more performant. Also,
[cols="20,80"]
|===
| Algorithms | Comments
| <<part04-algorithmic-toolbox#insertion-sort>> | Swap pairs bubbling up largest numbers to the right
| <<part04-algorithmic-toolbox#bubble-sort>> | Swap pairs bubbling up largest numbers to the right
| <<part04-algorithmic-toolbox#insertion-sort>> | Look for biggest number to the left and swap it with current
| <<part04-algorithmic-toolbox#selection-sort>> | Iterate array looking for smallest value to the right
| <<part04-algorithmic-toolbox#merge-sort>> | Split numbers in pairs, sort pairs and join them in ascending order
Expand All @@ -131,12 +131,11 @@ We explored many algorithms some of them simple and other more performant. Also,
.Sorting algorithms time/space complexity and properties
|===
| Algorithms | Avg | Best | Worst | Space | Stable | In-place | Online | Adaptive
| <<part04-algorithmic-toolbox#insertion-sort>> | O(n^2^) | O(n) | O(n^2^) | O(1) | Yes | Yes | Yes | Yes
| <<part04-algorithmic-toolbox#bubble-sort>> | O(n^2^) | O(n) | O(n^2^) | O(1) | Yes | Yes | Yes | Yes
| <<part04-algorithmic-toolbox#insertion-sort>> | O(n^2^) | O(n) | O(n^2^) | O(1) | Yes | Yes | Yes | Yes
| <<part04-algorithmic-toolbox#selection-sort>> | O(n^2^) | O(n^2^) | O(n^2^) | O(1) | No | Yes | No | No
| <<part04-algorithmic-toolbox#merge-sort>> | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | No | No | No
| <<part04-algorithmic-toolbox#quicksort>> | O(n log n) | O(n^2^) | O(n log n) | O(log n) | Yes | Yes | No | No
| <<part04-algorithmic-toolbox#quicksort>> | O(n log n) | O(n log n) | O(n^2^) | O(log n) | No | Yes | No | No
// | Tim sort | O(n log n) | O(log n) | Yes | No | No | Yes
|===
// end::table[]