From e03875da21dbc53e4ab69e3d1c05e1a55413d814 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Eray=20G=C3=BCndo=C4=9Fmu=C5=9F?= <74984741+eraygundogmus@users.noreply.github.com> Date: Mon, 13 Sep 2021 13:02:22 +0300 Subject: [PATCH 1/8] Update tree-intro.asc --- book/content/part03/tree-intro.asc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/content/part03/tree-intro.asc b/book/content/part03/tree-intro.asc index 80d65903..94f4b88b 100644 --- a/book/content/part03/tree-intro.asc +++ b/book/content/part03/tree-intro.asc @@ -106,6 +106,6 @@ image::image35.png[image,width=258,height=169] .Heap vs. Binary Search Tree **** -Heap is better at finding max or min values in constant time *O(1)*, while a balanced BST is good a finding any element in *O(log n)*. Heaps are often used to implement priority queues, while BST is used when you need every value sorted. +Heap is better at finding max or min values in constant time *O(1)*, while a balanced BST is good at finding any element in *O(log n)*. Heaps are often used to implement priority queues, while BST is used when you need every value sorted. **** indexterm:[Runtime, Logarithmic] From 5ac3cd9edd4cb18bb4379bfdaaa9c4f770a048b4 Mon Sep 17 00:00:00 2001 From: Ivan J Date: Tue, 9 Nov 2021 11:01:12 -0500 Subject: [PATCH 2/8] Update quick-sort.asc Fixing a minor but important detail --- book/content/part04/quick-sort.asc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/content/part04/quick-sort.asc b/book/content/part04/quick-sort.asc index 4f1cd5e0..e1a1de65 100644 --- a/book/content/part04/quick-sort.asc +++ b/book/content/part04/quick-sort.asc @@ -13,7 +13,7 @@ Quicksort is an efficient recursive sorting algorithm that uses <>. And, of course, It also outperforms simple sorting algorithms like <>, <> and <>. -Quicksort picks a "pivot" element randomly and moves all the smaller parts than the pivot to the right and the ones that are bigger to the left. It does this recursively until all the array is sorted. +Quicksort picks a "pivot" element randomly and moves all the smaller parts than the pivot to the left and the ones that are bigger to the right. It does this recursively until all the array is sorted. ===== Quicksort Implementation From ccfcfe6f039ce3f6e289d368fbdbf67e227c168c Mon Sep 17 00:00:00 2001 From: Joshua Morris Date: Sat, 27 Nov 2021 01:46:14 -0600 Subject: [PATCH 3/8] fix(graph): minor typo in bfs code documentation "bfs*" inline documentation should read "Breadth-first search" Fixes #110 --- src/data-structures/graphs/graph.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data-structures/graphs/graph.js b/src/data-structures/graphs/graph.js index 02b8d708..5342b6d3 100644 --- a/src/data-structures/graphs/graph.js +++ b/src/data-structures/graphs/graph.js @@ -134,7 +134,7 @@ class Graph { } /** - * Depth-first search + * Breadth-first search * Use a queue to visit nodes (FIFO) * @param {Node} first node to start the dfs */ From ae41553426a8d128db7958769bc8cfaa753acd23 Mon Sep 17 00:00:00 2001 From: semantic-release-bot Date: Tue, 30 Nov 2021 01:32:30 +0000 Subject: [PATCH 4/8] :bookmark: chore(release): 2.7.6 ## [2.7.6](https://github.com/amejiarosario/dsa.js/compare/2.7.5...2.7.6) (2021-11-30) ### Bug Fixes * **graph:** minor typo in bfs code documentation ([ccfcfe6](https://github.com/amejiarosario/dsa.js/commit/ccfcfe6f039ce3f6e289d368fbdbf67e227c168c)), closes [#110](https://github.com/amejiarosario/dsa.js/issues/110) --- CHANGELOG.md | 7 +++++++ package-lock.json | 4 ++-- package.json | 2 +- 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f1268e21..4e021d32 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [2.7.6](https://github.com/amejiarosario/dsa.js/compare/2.7.5...2.7.6) (2021-11-30) + + +### Bug Fixes + +* **graph:** minor typo in bfs code documentation ([ccfcfe6](https://github.com/amejiarosario/dsa.js/commit/ccfcfe6f039ce3f6e289d368fbdbf67e227c168c)), closes [#110](https://github.com/amejiarosario/dsa.js/issues/110) + ## [2.7.5](https://github.com/amejiarosario/dsa.js/compare/2.7.4...2.7.5) (2021-05-24) diff --git a/package-lock.json b/package-lock.json index 1d1cca05..961f44f2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "dsa.js", - "version": "2.7.5", + "version": "2.7.6", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "dsa.js", - "version": "2.7.4", + "version": "2.7.5", "license": "MIT", "devDependencies": { "@semantic-release/changelog": "^5.0.1", diff --git a/package.json b/package.json index 65e09fd3..73ef1563 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dsa.js", - "version": "2.7.5", + "version": "2.7.6", "description": "Data Structures & Algorithms in JS", "author": "Adrian Mejia (https://adrianmejia.com)", "homepage": "https://github.com/amejiarosario/dsa.js", From 5b49d8a8a12712328a8de9b94eba949d11553686 Mon Sep 17 00:00:00 2001 From: Gabriela Date: Thu, 19 May 2022 14:53:34 +0200 Subject: [PATCH 5/8] fix(runtimes/02-binary-search):fixes binary search iterative --- src/runtimes/02-binary-search.js | 44 ++++++--------------------- src/runtimes/02-binary-search.spec.js | 42 ++++++++++++++++++++----- 2 files changed, 44 insertions(+), 42 deletions(-) diff --git a/src/runtimes/02-binary-search.js b/src/runtimes/02-binary-search.js index 4e241b85..76203d31 100644 --- a/src/runtimes/02-binary-search.js +++ b/src/runtimes/02-binary-search.js @@ -38,50 +38,24 @@ function binarySearchRecursive(array, search, offset = 0) { * @param {string|number} search value to search for */ function binarySearchIterative(array, search) { - // console.log('binarySearchIterative', {array, search}); let start = 0; - let end = array.length; - const half = () => parseInt((end - start) / 2, 10) + start; + let end = array.length - 1; + const half = () => start + parseInt((end - start) / 2, 10); - while (end - start > 0) { + while (start <= end) { const currentIndex = half(); const current = array[currentIndex]; - if (current === search) { - return currentIndex; - } if (search > current) { - start = currentIndex; + if (current === search) return currentIndex; + + if (search > current) { + start = currentIndex + 1; } else if (search < current) { - end = currentIndex; + end = currentIndex - 1; } } return -1; } -// const binarySearch = binarySearchRecursive; -const binarySearch = binarySearchIterative; - -// function test() { -// const directory = ['Adrian', 'Bella', 'Charlotte', 'Daniel', -// 'Emma', 'Hanna', 'Isabella', 'Jayden', 'Kaylee', 'Luke', 'Mia', -// 'Nora', 'Olivia', 'Paisley', 'Riley', 'Thomas', 'Wyatt', 'Xander', 'Zoe']; -// -// const assert = require('assert'); -// assert.equal(binarySearch([], 'not found'), -1); -// assert.equal(binarySearch([1], 2), -1); -// assert.equal(binarySearch([1], 1), 0); -// assert.equal(binarySearch([1, 2, 3], 1), 0); -// assert.equal(binarySearch([1, 2, 3], 2), 1); -// assert.equal(binarySearch([1, 2, 3], 3), 2); -// assert.equal(binarySearch([1, 2, 3], 31), -1); -// assert.equal(binarySearch(directory, 'Adrian'), 0); -// assert.equal(binarySearch(directory, 'Hanna'), 5); -// assert.equal(binarySearch(directory, 'Zoe'), 18); -// assert.equal(binarySearch(directory, 'not found'), -1); -// } - -// test(); - - -module.exports = { binarySearch, binarySearchIterative, binarySearchRecursive }; +module.exports = { binarySearchIterative, binarySearchRecursive }; diff --git a/src/runtimes/02-binary-search.spec.js b/src/runtimes/02-binary-search.spec.js index 4850cd7c..e6aca91b 100644 --- a/src/runtimes/02-binary-search.spec.js +++ b/src/runtimes/02-binary-search.spec.js @@ -1,6 +1,6 @@ -const binarySearch = require('./02-binary-search').binarySearchRecursive; +const { binarySearchRecursive, binarySearchIterative } = require('./02-binary-search'); -describe('Binary Search', () => { +describe('Binary Search Recursive', () => { let array; beforeEach(() => { @@ -8,22 +8,50 @@ describe('Binary Search', () => { }); it('should find a middle element', () => { - expect(binarySearch(array, 9)).toEqual(1); + expect(binarySearchRecursive(array, 9)).toEqual(1); }); it('should find an first element', () => { - expect(binarySearch(array, 7)).toEqual(0); + expect(binarySearchRecursive(array, 7)).toEqual(0); }); it('should find the last element', () => { - expect(binarySearch(array, 23)).toEqual(3); + expect(binarySearchRecursive(array, 23)).toEqual(3); }); it('should not find an bigger element', () => { - expect(binarySearch(array, 9000)).toEqual(-1); + expect(binarySearchRecursive(array, 9000)).toEqual(-1); }); it('should find a smaller element', () => { - expect(binarySearch(array, -9)).toEqual(-1); + expect(binarySearchRecursive(array, -9)).toEqual(-1); + }); +}); + +describe('Binary Search Iterative', () => { + let array; + + beforeEach(() => { + array = [7, 9, 13, 23]; + }); + + it('should find a middle element', () => { + expect(binarySearchIterative(array, 9)).toEqual(1); + }); + + it('should find an first element', () => { + expect(binarySearchIterative(array, 7)).toEqual(0); + }); + + it('should find the last element', () => { + expect(binarySearchIterative(array, 23)).toEqual(3); + }); + + it('should not find an bigger element', () => { + expect(binarySearchIterative(array, 9000)).toEqual(-1); + }); + + it('should find a smaller element', () => { + expect(binarySearchIterative(array, -9)).toEqual(-1); }); }); From cde6e8a2291bb97e08b28dedf093942e664eaa09 Mon Sep 17 00:00:00 2001 From: Balaji Saravanan Date: Sun, 29 May 2022 12:01:08 +0530 Subject: [PATCH 6/8] Update heap.js Fix proper parent index fetching --- src/data-structures/heaps/heap.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/data-structures/heaps/heap.js b/src/data-structures/heaps/heap.js index 54863c39..537894ee 100644 --- a/src/data-structures/heaps/heap.js +++ b/src/data-structures/heaps/heap.js @@ -59,7 +59,7 @@ class Heap { */ bubbleUp() { let index = this.size - 1; - const parent = (i) => Math.ceil(i / 2 - 1); + const parent = (i) => Math.ceil(i / 2) - 1; while (parent(index) >= 0 && this.comparator(parent(index), index) > 0) { this.swap(parent(index), index); index = parent(index); From c9249d38cc3809bc2c0457f35494772cb57b28b9 Mon Sep 17 00:00:00 2001 From: Adrian Mejia Date: Sat, 10 Dec 2022 16:02:51 -0500 Subject: [PATCH 7/8] chore(binary-search): consolidate test for multiple implementations --- src/runtimes/02-binary-search.js | 6 +- src/runtimes/02-binary-search.spec.js | 92 +++++++++++---------------- 2 files changed, 41 insertions(+), 57 deletions(-) diff --git a/src/runtimes/02-binary-search.js b/src/runtimes/02-binary-search.js index 76203d31..c8118160 100644 --- a/src/runtimes/02-binary-search.js +++ b/src/runtimes/02-binary-search.js @@ -18,9 +18,11 @@ function binarySearchRecursive(array, search, offset = 0) { if (current === search) { return offset + half; - } if (array.length === 1) { + } + if (array.length === 1) { return -1; - } if (search > current) { + } + if (search > current) { const right = array.slice(half); return binarySearchRecursive(right, search, offset + half); } diff --git a/src/runtimes/02-binary-search.spec.js b/src/runtimes/02-binary-search.spec.js index e6aca91b..1e8f8f56 100644 --- a/src/runtimes/02-binary-search.spec.js +++ b/src/runtimes/02-binary-search.spec.js @@ -1,57 +1,39 @@ -const { binarySearchRecursive, binarySearchIterative } = require('./02-binary-search'); - -describe('Binary Search Recursive', () => { - let array; - - beforeEach(() => { - array = [7, 9, 13, 23]; - }); - - it('should find a middle element', () => { - expect(binarySearchRecursive(array, 9)).toEqual(1); - }); - - it('should find an first element', () => { - expect(binarySearchRecursive(array, 7)).toEqual(0); - }); - - it('should find the last element', () => { - expect(binarySearchRecursive(array, 23)).toEqual(3); - }); - - it('should not find an bigger element', () => { - expect(binarySearchRecursive(array, 9000)).toEqual(-1); - }); - - it('should find a smaller element', () => { - expect(binarySearchRecursive(array, -9)).toEqual(-1); - }); -}); - -describe('Binary Search Iterative', () => { - let array; - - beforeEach(() => { - array = [7, 9, 13, 23]; - }); - - it('should find a middle element', () => { - expect(binarySearchIterative(array, 9)).toEqual(1); - }); - - it('should find an first element', () => { - expect(binarySearchIterative(array, 7)).toEqual(0); - }); - - it('should find the last element', () => { - expect(binarySearchIterative(array, 23)).toEqual(3); - }); - - it('should not find an bigger element', () => { - expect(binarySearchIterative(array, 9000)).toEqual(-1); - }); - - it('should find a smaller element', () => { - expect(binarySearchIterative(array, -9)).toEqual(-1); +const { + binarySearchRecursive, + binarySearchIterative, +} = require("./02-binary-search"); + +const binarySearchImplementations = [ + binarySearchRecursive, + binarySearchIterative, +]; + +binarySearchImplementations.forEach((binarySearchImpl) => { + describe(binarySearchImpl.name, () => { + let array; + + beforeEach(() => { + array = [7, 9, 13, 23]; + }); + + it("should find a middle element", () => { + expect(binarySearchImpl(array, 9)).toEqual(1); + }); + + it("should find an first element", () => { + expect(binarySearchImpl(array, 7)).toEqual(0); + }); + + it("should find the last element", () => { + expect(binarySearchImpl(array, 23)).toEqual(3); + }); + + it("should not find an bigger element", () => { + expect(binarySearchImpl(array, 9000)).toEqual(-1); + }); + + it("should find a smaller element", () => { + expect(binarySearchImpl(array, -9)).toEqual(-1); + }); }); }); From fefb06a6b51a7699cb3986ed2d20dc0d03511055 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ca=C3=ADque=20de=20Castro=20Soares=20da=20Silva?= Date: Mon, 26 Dec 2022 17:45:05 -0300 Subject: [PATCH 8/8] Fix return of unshift method Fix return of unshift method --- book/content/part02/array.asc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/book/content/part02/array.asc b/book/content/part02/array.asc index 3bde2929..25c029ee 100644 --- a/book/content/part02/array.asc +++ b/book/content/part02/array.asc @@ -73,7 +73,7 @@ Here's an example: [source, javascript] ---- const array = [2, 5, 1]; -array.unshift(0); // ↪️ 8 +array.unshift(0); // ↪️ 4 console.log(array); // [ 0, 2, 5, 1 ] array.unshift(-2, -1); // ↪️ 6 console.log(array); // [ -2, -1, 0, 2, 5, 1 ]