From f290d2d322fff078ea890943fc9645be596d7be4 Mon Sep 17 00:00:00 2001 From: Nour B <56294154+nourrrrrrrr@users.noreply.github.com> Date: Sat, 25 Apr 2020 13:10:06 +0100 Subject: [PATCH 1/6] HeapSort algorithm --- Sorts/Heapsort.js | 48 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 Sorts/Heapsort.js diff --git a/Sorts/Heapsort.js b/Sorts/Heapsort.js new file mode 100644 index 0000000000..579f596976 --- /dev/null +++ b/Sorts/Heapsort.js @@ -0,0 +1,48 @@ + var array_length; +/* to create MAX array */ +function heap_root(input, i) { + var left = 2 * i + 1; + var right = 2 * i + 2; + var max = i; + + if (left < array_length && input[left] > input[max]) { + max = left; + } + + if (right < array_length && input[right] > input[max]) { + max = right; + } + + if (max != i) { + swap(input, i, max); + heap_root(input, max); + } +} + +function swap(input, index_A, index_B) { + var temp = input[index_A]; + + input[index_A] = input[index_B]; + input[index_B] = temp; +} + +function heapSort(input) { + + array_length = input.length; + + for (var i = Math.floor(array_length / 2); i >= 0; i -= 1) { + heap_root(input, i); + } + + for (i = input.length - 1; i > 0; i--) { + swap(input, 0, i); + array_length--; + + + heap_root(input, 0); + } +} + +var arr = [3, 0, 2, 5, -1, 4, 1]; +heapSort(arr); +console.log(arr); From 2adba068393d210f9ae9e3e51d740ee0591335b6 Mon Sep 17 00:00:00 2001 From: Nour B <56294154+nourrrrrrrr@users.noreply.github.com> Date: Wed, 29 Apr 2020 11:17:20 +0100 Subject: [PATCH 2/6] Create QuickSelect.js --- QuickSelect.js | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 QuickSelect.js diff --git a/QuickSelect.js b/QuickSelect.js new file mode 100644 index 0000000000..b2ccb104ab --- /dev/null +++ b/QuickSelect.js @@ -0,0 +1,66 @@ +/** + * QuickSelect is an algorithm to find the kth smallest number + * + * Notes: + * -QuickSelect is related to QuickSort, thus has optimal best and average + * case (O(n)) but unlikely poor worst case (O(n^2)) + * -This implementation uses randomly selected pivots for better performance + * + * @complexity: O(n) (on average ) + * @complexity: O(n^2) (worst case) + * @flow + */ +type num = number; + +export default function QuickSelect(items: num[], kth: num): num { + return RandomizedSelect(items, 0, items.length - 1, kth); +} + +export function RandomizedSelect( + items: num[], + left: num, + right: num, + i: num +): any { + if (left === right) return items[left]; + + const pivotIndex = RandomizedPartition(items, left, right); + const k = pivotIndex - left + 1; + + if (i === k) return items[pivotIndex]; + if (i < k) return RandomizedSelect(items, left, pivotIndex - 1, i); + + return RandomizedSelect(items, pivotIndex + 1, right, i - k); +} + +export function RandomizedPartition(items: num[], left: num, right: num): num { + const rand = getRandomInt(left, right); + Swap(items, rand, right); + return Partition(items, left, right); +} + +function Partition(items: num[], left: num, right: num): num { + const x = items[right]; + let pivotIndex = left - 1; + + for (let j = left; j < right; j++) { + if (items[j] <= x) { + pivotIndex++; + Swap(items, pivotIndex, j); + } + } + + Swap(items, pivotIndex + 1, right); + + return pivotIndex + 1; +} + +function getRandomInt(min: num, max: num): num { + return Math.floor(Math.random() * (max - min + 1)) + min; +} + +function Swap(arr: any[], x: any, y: any) { + const temp = arr[x]; + arr[x] = arr[y]; + arr[y] = temp; +} From 331bc579b2f1adab201ce433cef28d4e41786d20 Mon Sep 17 00:00:00 2001 From: Nour B <56294154+nourrrrrrrr@users.noreply.github.com> Date: Fri, 1 May 2020 13:03:47 +0100 Subject: [PATCH 3/6] Algorithm to reverse a string. --- maths/ReverseString.js | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 maths/ReverseString.js diff --git a/maths/ReverseString.js b/maths/ReverseString.js new file mode 100644 index 0000000000..8ac4fdf667 --- /dev/null +++ b/maths/ReverseString.js @@ -0,0 +1,39 @@ +/** + * A short example showing how to reverse a string + * @flow + */ + +/** + * Create a new string and append + * @complexity O(n) + */ +export default function ReverseStringIterative(string: string): string { + let reversedString = ''; + let index; + + for (index = string.length - 1; index >= 0; index--) { + reversedString += string[index]; + } + + return reversedString; +} + +/** + * JS disallows string mutation so we're actually a bit slower. + * + * @complexity: O(n) + * + * 'some' -> 'eoms' -> 'emos' + */ +export function ReverseStringIterativeInplace(string: string): string { + const _string = string.split(''); + + for (let i = 0; i < Math.floor(_string.length / 2); i++) { + const first = _string[i]; + const second = _string[_string.length - 1 - i]; + _string[i] = second; + _string[_string.length - 1 - i] = first; + } + + return _string.join(''); +} From 9c50d755264a2e15583ffd199fc46afc3a468555 Mon Sep 17 00:00:00 2001 From: vinayak Date: Thu, 7 May 2020 20:37:21 +0530 Subject: [PATCH 4/6] Update ReverseString.js --- maths/ReverseString.js | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/maths/ReverseString.js b/maths/ReverseString.js index 8ac4fdf667..8709e17df9 100644 --- a/maths/ReverseString.js +++ b/maths/ReverseString.js @@ -7,15 +7,16 @@ * Create a new string and append * @complexity O(n) */ -export default function ReverseStringIterative(string: string): string { - let reversedString = ''; - let index; + +function ReverseStringIterative (string) { + let reversedString = '' + let index for (index = string.length - 1; index >= 0; index--) { - reversedString += string[index]; + reversedString += string[index] } - return reversedString; + return reversedString } /** @@ -25,15 +26,20 @@ export default function ReverseStringIterative(string: string): string { * * 'some' -> 'eoms' -> 'emos' */ -export function ReverseStringIterativeInplace(string: string): string { - const _string = string.split(''); + +function ReverseStringIterativeInplace (string) { + const _string = string.split('') for (let i = 0; i < Math.floor(_string.length / 2); i++) { - const first = _string[i]; - const second = _string[_string.length - 1 - i]; - _string[i] = second; - _string[_string.length - 1 - i] = first; + const first = _string[i] + const second = _string[_string.length - 1 - i] + _string[i] = second + _string[_string.length - 1 - i] = first } - return _string.join(''); + return _string.join('') } + +// testing +console.log(ReverseStringIterative('Javascript')) +console.log(ReverseStringIterativeInplace('Javascript')) From ba2c3652bc7a079325be5ee059635ea1ccb84329 Mon Sep 17 00:00:00 2001 From: vinayak Date: Thu, 7 May 2020 20:44:52 +0530 Subject: [PATCH 5/6] Update Heapsort.js --- Sorts/Heapsort.js | 82 +++++++++++++++++++++++------------------------ 1 file changed, 41 insertions(+), 41 deletions(-) diff --git a/Sorts/Heapsort.js b/Sorts/Heapsort.js index 579f596976..d250d41ea9 100644 --- a/Sorts/Heapsort.js +++ b/Sorts/Heapsort.js @@ -1,48 +1,48 @@ - var array_length; -/* to create MAX array */ -function heap_root(input, i) { - var left = 2 * i + 1; - var right = 2 * i + 2; - var max = i; - - if (left < array_length && input[left] > input[max]) { - max = left; - } - - if (right < array_length && input[right] > input[max]) { - max = right; - } - - if (max != i) { - swap(input, i, max); - heap_root(input, max); - } +let arrayLength = 0 + +/* to create MAX array */ + +function heapRoot (input, i) { + const left = 2 * i + 1 + const right = 2 * i + 2 + let max = i + + if (left < arrayLength && input[left] > input[max]) { + max = left + } + + if (right < arrayLength && input[right] > input[max]) { + max = right + } + + if (max !== i) { + swap(input, i, max) + heapRoot(input, max) + } } -function swap(input, index_A, index_B) { - var temp = input[index_A]; +function swap (input, indexA, indexB) { + const temp = input[indexA] - input[index_A] = input[index_B]; - input[index_B] = temp; + input[indexA] = input[indexB] + input[indexB] = temp } -function heapSort(input) { - - array_length = input.length; - - for (var i = Math.floor(array_length / 2); i >= 0; i -= 1) { - heap_root(input, i); - } - - for (i = input.length - 1; i > 0; i--) { - swap(input, 0, i); - array_length--; - - - heap_root(input, 0); - } +function heapSort (input) { + arrayLength = input.length + + for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) { + heapRoot(input, i) + } + + for (let i = input.length - 1; i > 0; i--) { + swap(input, 0, i) + arrayLength-- + + heapRoot(input, 0) + } } -var arr = [3, 0, 2, 5, -1, 4, 1]; -heapSort(arr); -console.log(arr); +const arr = [3, 0, 2, 5, -1, 4, 1] +heapSort(arr) +console.log(arr) From da9ded5d2fb1a4c01c033954d1e47059468b625e Mon Sep 17 00:00:00 2001 From: vinayak Date: Thu, 7 May 2020 20:52:37 +0530 Subject: [PATCH 6/6] Update QuickSelect.js --- QuickSelect.js | 66 ++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 32 deletions(-) diff --git a/QuickSelect.js b/QuickSelect.js index b2ccb104ab..473a577a94 100644 --- a/QuickSelect.js +++ b/QuickSelect.js @@ -10,57 +10,59 @@ * @complexity: O(n^2) (worst case) * @flow */ -type num = number; -export default function QuickSelect(items: num[], kth: num): num { - return RandomizedSelect(items, 0, items.length - 1, kth); +function QuickSelect (items, kth) { + return RandomizedSelect(items, 0, items.length - 1, kth) } -export function RandomizedSelect( - items: num[], - left: num, - right: num, - i: num -): any { - if (left === right) return items[left]; +function RandomizedSelect ( + items, + left, + right, + i +) { + if (left === right) return items[left] - const pivotIndex = RandomizedPartition(items, left, right); - const k = pivotIndex - left + 1; + const pivotIndex = RandomizedPartition(items, left, right) + const k = pivotIndex - left + 1 - if (i === k) return items[pivotIndex]; - if (i < k) return RandomizedSelect(items, left, pivotIndex - 1, i); + if (i === k) return items[pivotIndex] + if (i < k) return RandomizedSelect(items, left, pivotIndex - 1, i) - return RandomizedSelect(items, pivotIndex + 1, right, i - k); + return RandomizedSelect(items, pivotIndex + 1, right, i - k) } -export function RandomizedPartition(items: num[], left: num, right: num): num { - const rand = getRandomInt(left, right); - Swap(items, rand, right); - return Partition(items, left, right); +function RandomizedPartition (items, left, right) { + const rand = getRandomInt(left, right) + Swap(items, rand, right) + return Partition(items, left, right) } -function Partition(items: num[], left: num, right: num): num { - const x = items[right]; - let pivotIndex = left - 1; +function Partition (items, left, right) { + const x = items[right] + let pivotIndex = left - 1 for (let j = left; j < right; j++) { if (items[j] <= x) { - pivotIndex++; - Swap(items, pivotIndex, j); + pivotIndex++ + Swap(items, pivotIndex, j) } } - Swap(items, pivotIndex + 1, right); + Swap(items, pivotIndex + 1, right) - return pivotIndex + 1; + return pivotIndex + 1 } -function getRandomInt(min: num, max: num): num { - return Math.floor(Math.random() * (max - min + 1)) + min; +function getRandomInt (min, max) { + return Math.floor(Math.random() * (max - min + 1)) + min } -function Swap(arr: any[], x: any, y: any) { - const temp = arr[x]; - arr[x] = arr[y]; - arr[y] = temp; +function Swap (arr, x, y) { + const temp = arr[x] + arr[x] = arr[y] + arr[y] = temp } + +// testing +console.log(QuickSelect([1, 4, 2, -2, 4, 5]))