Skip to content

Commit 9874647

Browse files
Merge pull request #63 from amejiarosario/hotfix/sorting
Hotfix/sorting
2 parents f484075 + f3fe049 commit 9874647

File tree

7 files changed

+73
-7
lines changed

7 files changed

+73
-7
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Brute force: O(n^2) | O(1)
2+
function twoSum(nums, target) {
3+
for (let i = 0; i < nums.length - 1; i++) { // O(n^2)
4+
for (let j = i + 1; j < nums.length; j++) { // O(n)
5+
if (nums[i] + nums[j] === target) {
6+
return [i, j];
7+
}
8+
}
9+
}
10+
return [];
11+
}
12+
13+
module.exports = twoSum;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// With a HashMap: O(n) | O(n)
2+
function twoSum(nums, target) {
3+
const map = nums.reduce((m, v, i) => { // O(n)
4+
const ids = m.get(v) || [];
5+
ids.push(i);
6+
return m.set(v, ids);
7+
}, new Map());
8+
9+
for (let i = 0; i < nums.length; i++) { // O(n)
10+
const diff = target - nums[i];
11+
if (map.has(diff) && i !== map.get(diff)) {
12+
return [i, map.get(diff)];
13+
}
14+
}
15+
16+
return [];
17+
}
18+
19+
module.exports = twoSum;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// With a HashMap: O(n) | O(n), 1-pass
2+
function twoSum(nums, target) {
3+
const map = new Map();
4+
5+
for (let i = 0; i < nums.length; i++) { // O(n)
6+
const complement = target - nums[i];
7+
8+
if (map.has(complement)) {
9+
return [map.get(complement), i];
10+
}
11+
12+
map.set(nums[i], i);
13+
}
14+
15+
return [];
16+
}
17+
18+
module.exports = twoSum;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const assert = require('assert');
2+
3+
const twoSum = require('./01-two-sum');
4+
5+
function test() {
6+
assert.deepEqual(twoSum([1, 2, 3], 4), [0, 2]);
7+
assert.deepEqual(twoSum([1, 2, 3], 14), []);
8+
9+
assert.deepEqual(twoSum([2, 2, 2], 4), [0, 1]);
10+
assert.deepEqual(twoSum(Array(1e7).fill(2), 4), [0, 1]); //
11+
// 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
12+
// assert.deepEqual(twoSum(Array(1e9).fill(2), 4), [0, 1]); // 1e7 - error 137 - OUT OF MEMORY
13+
console.log('All tests passed!');
14+
}
15+
16+
test();

book/content/part04/bubble-sort.asc

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ ifndef::imagesdir[]
33
:codedir: ../../../src
44
endif::[]
55

6+
[[bubble-sort]]
67
==== Bubble Sort
78
(((Bubble Sort)))
89
(((Sorting, Bubble Sort)))

book/content/part04/quick-sort.asc

+2-2
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,11 @@ With the optimization, Quicksort has an _O(n log n)_ running time. Similar to th
8383
- <<Online>>: [big]#️❌# No, the pivot element can be choose at random.
8484
- Recursive: Yes
8585
- Time Complexity: [big]#✅# <<part01-algorithms-analysis#linearithmic>> _O(n log n)_
86-
- Space Complexity: [big]#✅# <<part01-algorithms-analysis#constant>> _O(1)_
86+
- Space Complexity: [big]#✅# <<part01-algorithms-analysis#logarithmic>> _O(log n)_, because of recursion.
8787

8888
(((Linearithmic)))
8989
(((Runtime, Linearithmic)))
90-
(((Space complexity, Constant)))
90+
(((Space complexity, Logarithmic)))
9191

9292
// Resources:
9393
// https://www.khanacademy.org/computing/computer-science/algorithms/quick-sort/a/linear-time-partitioning

book/content/part04/sorting-algorithms.asc

+4-5
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Sorting is one of the most common solutions when we want to extract some insight
99
We can sort to get the maximum or minimum value and many algorithmic problems involves sorting data first.
1010

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

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

0 commit comments

Comments
 (0)