From 6ed6fda2be9c2343422047aec0c49ccf24100916 Mon Sep 17 00:00:00 2001 From: syedjafer Date: Thu, 13 Oct 2022 21:28:17 +0530 Subject: [PATCH 1/3] binary insertion sort --- Maths/CollatzSequence.js | 2 +- Sorts/BinaryInsertionSort.js | 60 ++++++++++++++++++++++++++ Sorts/test/BinaryInsertionSort.test.js | 8 ++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 Sorts/BinaryInsertionSort.js create mode 100644 Sorts/test/BinaryInsertionSort.test.js diff --git a/Maths/CollatzSequence.js b/Maths/CollatzSequence.js index d8ead61cc2..4a3566b216 100644 --- a/Maths/CollatzSequence.js +++ b/Maths/CollatzSequence.js @@ -26,5 +26,5 @@ export function collatz (n) { steps.push(n) } - return { result: n, steps: steps } + return { result: n, steps } } diff --git a/Sorts/BinaryInsertionSort.js b/Sorts/BinaryInsertionSort.js new file mode 100644 index 0000000000..b522fa63a9 --- /dev/null +++ b/Sorts/BinaryInsertionSort.js @@ -0,0 +1,60 @@ +/** + * Pure Implementation of Binary Search Algorithm + * + * Binary insertion sort is a sorting algorithm similar to insertion sort, + * but instead of using linear search to find the position + * where the element should be inserted, we use binary search. + * Thus, we reduce the number of comparisons for inserting one element from O(N) + * (Time complexity in Insertion Sort) to O(log N). + * + */ + +/** + * Search the key element in the array from start position to end position. + * + * @param {Array} array Array of numbers. + * @param {Number} key Value to be searched + * @param {Number} start start index position of array + * @param {Number} end end index position of array + * @return {Number} Position of the key element + */ +function binarySearch (array, key, start, end) { + if (start === end) { + if (array[start] > key) { + return start + } else { + return start + 1 + } + } + + if (start > end) { + return start + } + + const mid = Math.floor((start + end) / 2) + + if (array[mid] < key) { + return binarySearch(array, key, mid + 1, end) + } else if (array[mid] > key) { + return binarySearch(array, key, start, mid - 1) + } else { + return mid + } +} + +/** + * Binary Insertion Sort + * + * @param {Array} list List to be sorted. + * @return {Array} The sorted list. + */ +export function binaryInsertionSort (array) { + const totalLength = array.length + for (let itr = 1; itr < totalLength; itr += 1) { + const key = array[itr] + const indexPosition = binarySearch(array, key, 0, itr - 1) + array.splice(itr, 1) + array.splice(indexPosition, 0, key) + } + return array +} diff --git a/Sorts/test/BinaryInsertionSort.test.js b/Sorts/test/BinaryInsertionSort.test.js new file mode 100644 index 0000000000..84abe6a7ce --- /dev/null +++ b/Sorts/test/BinaryInsertionSort.test.js @@ -0,0 +1,8 @@ +import { binaryInsertionSort } from '../BinaryInsertionSort' + +describe('BinaryInsertionSort', () => { + it('should sort arrays correctly', () => { + expect(binaryInsertionSort([5, 4, 3, 2, 1])).toEqual([1, 2, 3, 4, 5]) + expect(binaryInsertionSort([7, 9, 4, 3, 5])).toEqual([3, 4, 5, 7, 9]) + }) +}) From 954624aa90ee185e5d26da6b309b132e90efaaf6 Mon Sep 17 00:00:00 2001 From: syedjafer Date: Thu, 13 Oct 2022 22:11:21 +0530 Subject: [PATCH 2/3] Resolved Comments --- Sorts/BinaryInsertionSort.js | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/Sorts/BinaryInsertionSort.js b/Sorts/BinaryInsertionSort.js index b522fa63a9..3326404e40 100644 --- a/Sorts/BinaryInsertionSort.js +++ b/Sorts/BinaryInsertionSort.js @@ -43,17 +43,17 @@ function binarySearch (array, key, start, end) { } /** - * Binary Insertion Sort - * - * @param {Array} list List to be sorted. - * @return {Array} The sorted list. - */ + * Binary Insertion Sort + * + * @param {Array} list List to be sorted. + * @return {Array} The sorted list. + */ export function binaryInsertionSort (array) { const totalLength = array.length - for (let itr = 1; itr < totalLength; itr += 1) { - const key = array[itr] - const indexPosition = binarySearch(array, key, 0, itr - 1) - array.splice(itr, 1) + for (let i = 1; i < totalLength; i += 1) { + const key = array[i] + const indexPosition = binarySearch(array, key, 0, i - 1) + array.splice(i, 1) array.splice(indexPosition, 0, key) } return array From 128f43585b537d27229b576797c248b87211f991 Mon Sep 17 00:00:00 2001 From: syedjafer Date: Sun, 16 Oct 2022 06:30:41 +0530 Subject: [PATCH 3/3] Resolving Commits --- Maths/CollatzSequence.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Maths/CollatzSequence.js b/Maths/CollatzSequence.js index 4a3566b216..d8ead61cc2 100644 --- a/Maths/CollatzSequence.js +++ b/Maths/CollatzSequence.js @@ -26,5 +26,5 @@ export function collatz (n) { steps.push(n) } - return { result: n, steps } + return { result: n, steps: steps } }