diff --git a/Search/BinarySearch.js b/Search/BinarySearch.js index 4d4789d65..ce6571d55 100644 --- a/Search/BinarySearch.js +++ b/Search/BinarySearch.js @@ -49,42 +49,43 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) { return -1 } +export { binarySearchIterative, binarySearchRecursive} + /* ---------------------------------- Test ---------------------------------- */ -const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] -const stringArr = [ - 'Alpha', - 'Bravo', - 'Charlie', - 'Delta', - 'Echo', - 'Foxtrot', - 'Golf', - 'Hotel', - 'India', - 'Juliet', - 'Kilo', - 'Lima', - 'Mike', - 'November', - 'Oscar', - 'Papa', - 'Quebec', - 'Romeo', - 'Sierra', - 'Tango', - 'Uniform', - 'Victor', - 'Whiskey', - 'X-Ray', - 'Yankee', - 'Zulu' -] +// const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +// const stringArr = [ +// 'Alpha', +// 'Bravo', +// 'Charlie', +// 'Delta', +// 'Echo', +// 'Foxtrot', +// 'Golf', +// 'Hotel', +// 'India', +// 'Juliet', +// 'Kilo', +// 'Lima', +// 'Mike', +// 'November', +// 'Oscar', +// 'Papa', +// 'Quebec', +// 'Romeo', +// 'Sierra', +// 'Tango', +// 'Uniform', +// 'Victor', +// 'Whiskey', +// 'X-Ray', +// 'Yankee', +// 'Zulu' +// ] -console.log(binarySearchRecursive(arr, 3)) -console.log(binarySearchIterative(arr, 7)) -console.log(binarySearchRecursive(arr, 13)) - -console.log(binarySearchIterative(stringArr, 'Charlie')) -console.log(binarySearchRecursive(stringArr, 'Zulu')) -console.log(binarySearchIterative(stringArr, 'Sierra')) +// binarySearchRecursive(arr, 3) +// binarySearchIterative(arr, 7) +// binarySearchRecursive(arr, 13) +// binarySearchIterative(stringArr, 'Charlie') +// binarySearchRecursive(stringArr, 'Zulu') +// binarySearchIterative(stringArr, 'Sierra') diff --git a/Search/ExponentialSearch.js b/Search/ExponentialSearch.js index e46a5ee89..a35aa363c 100644 --- a/Search/ExponentialSearch.js +++ b/Search/ExponentialSearch.js @@ -47,12 +47,8 @@ function exponentialSearch (arr, length, value) { return binarySearch(arr, value, i / 2, Math.min(i, length)) } -const arr = [2, 3, 4, 10, 40, 65, 78, 100] -const value = 78 -const result = exponentialSearch(arr, arr.length, value) +export { binarySearch, exponentialSearch} -if (result < 0) { - console.log('Element not found') -} else { - console.log('Element found at position :' + result) -} +// const arr = [2, 3, 4, 10, 40, 65, 78, 100] +// const value = 78 +// const result = exponentialSearch(arr, arr.length, value) diff --git a/Search/FibonacciSearch.js b/Search/FibonacciSearch.js index fcc8c8561..e1c3060d8 100644 --- a/Search/FibonacciSearch.js +++ b/Search/FibonacciSearch.js @@ -19,7 +19,7 @@ * the item (number) to be searched for and the length of the items in the array ****************************************************************************/ -const fibonacciSearch = (arr, x, n) => { +export const fibonacciSearch = (arr, x, n) => { let fib2 = 0 // (K-2)'th Fibonacci Number let fib1 = 1 // (K-1)'th Fibonacci Number. let fibK = fib2 + fib1 // Kth Fibonacci @@ -69,9 +69,9 @@ const fibonacciSearch = (arr, x, n) => { // element not found. return -1 return -1 } + // Example -const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100] -const n = myArray.length -const x = 90 -const fibFinder = fibonacciSearch(myArray, x, n) -console.log('Element found at index:', fibFinder) +// const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100] +// const n = myArray.length +// const x = 90 +// const fibFinder = fibonacciSearch(myArray, x, n) diff --git a/Search/InterpolationSearch.js b/Search/InterpolationSearch.js index c256fd8c7..6ca3eea96 100644 --- a/Search/InterpolationSearch.js +++ b/Search/InterpolationSearch.js @@ -9,7 +9,7 @@ * */ -function interpolationSearch (arr, key) { +export function interpolationSearch (arr, key) { const length = arr.length - 1 let low = 0 let high = length @@ -38,9 +38,9 @@ function interpolationSearch (arr, key) { return -1 } -const arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39] +// const arr = [2, 6, 8, 10, 12, 14, 16, 18, 20, 22, 26, 34, 39] -console.log('Found at position :' + interpolationSearch(arr, 2)) -console.log('Found at position :' + interpolationSearch(arr, 12)) -console.log('Found at position :' + interpolationSearch(arr, 1000)) -console.log('Found at position :' + interpolationSearch(arr, 39)) +// interpolationSearch(arr, 2) +// interpolationSearch(arr, 12) +// interpolationSearch(arr, 1000) +// interpolationSearch(arr, 39) diff --git a/Search/LinearSearch.js b/Search/LinearSearch.js index 6b4211d40..f6416f6d6 100644 --- a/Search/LinearSearch.js +++ b/Search/LinearSearch.js @@ -21,7 +21,9 @@ function Search (theArray, key) { return -1 } -const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9] -SearchArray(3, ar) -SearchArray(4, ar) -SearchArray(11, ar) +export { SearchArray, Search } + +// const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9] +// SearchArray(3, ar) +// SearchArray(4, ar) +// SearchArray(11, ar) diff --git a/Search/QuickSelectSearch.js b/Search/QuickSelectSearch.js index 7a8c57bd3..8ae530501 100644 --- a/Search/QuickSelectSearch.js +++ b/Search/QuickSelectSearch.js @@ -11,7 +11,7 @@ * * [Reference](http://en.wikipedia.org/wiki/Quickselect) */ -function quickSelectSearch (array, k) { +export function quickSelectSearch (array, k) { if (!array || array.length <= k) { throw new Error('Invalid arguments') } @@ -49,7 +49,7 @@ function quickSelectSearch (array, k) { /* ---------------------------------- Test ---------------------------------- */ -const arr = [1121111, 21, 333, 41, 5, 66, 7777, 28, 19, 11110] -console.log(quickSelectSearch(arr, 5)) // [ 19, 21, 28, 41, 5, 66, 333, 11110, 1121111, 7777 ] -console.log(quickSelectSearch(arr, 2)) // [ 19, 5, 21, 41, 28, 333, 11110, 1121111, 7777, 66 ] -console.log(quickSelectSearch(arr, 7)) // [ 19, 5, 21, 41, 28, 66, 333, 7777, 11110, 1121111 ] +// const arr = [1121111, 21, 333, 41, 5, 66, 7777, 28, 19, 11110] +// quickSelectSearch(arr, 5) // [ 19, 21, 28, 41, 5, 66, 333, 11110, 1121111, 7777 ] +// quickSelectSearch(arr, 2) // [ 19, 5, 21, 41, 28, 333, 11110, 1121111, 7777, 66 ] +// quickSelectSearch(arr, 7) // [ 19, 5, 21, 41, 28, 66, 333, 7777, 11110, 1121111 ] diff --git a/Search/StringSearch.js b/Search/StringSearch.js index 614575107..634f3979b 100644 --- a/Search/StringSearch.js +++ b/Search/StringSearch.js @@ -35,7 +35,7 @@ function makeTable (str) { } // Find all the words that matches in a given string `str` -function stringSearch (str, word) { +export function stringSearch (str, word) { // find the prefix table in O(n) const prefixes = makeTable(word) const matches = [] @@ -80,4 +80,4 @@ function stringSearch (str, word) { return matches } -console.log(stringSearch('Hello search the position of me', 'pos')) +// stringSearch('Hello search the position of me', 'pos') diff --git a/Sorts/CountingSort.js b/Sorts/CountingSort.js index a31ea4455..c7d495d92 100644 --- a/Sorts/CountingSort.js +++ b/Sorts/CountingSort.js @@ -8,7 +8,7 @@ * Animated Visual: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html */ -const countingSort = (arr, min, max) => { +export const countingSort = (arr, min, max) => { // Create an auxiliary resultant array const res = [] // Create and initialize the frequency[count] array @@ -33,11 +33,5 @@ const countingSort = (arr, min, max) => { /** * Implementation of Counting Sort */ -const array = [3, 0, 2, 5, 4, 1] -// Before Sort -console.log('\n- Before Sort | Implementation of Counting Sort -') -console.log(array) -// After Sort -console.log('- After Sort | Implementation of Counting Sort -') -console.log(countingSort(array, 0, 5)) -console.log('\n') +// const array = [3, 0, 2, 5, 4, 1] +// countingSort(array, 0, 5) diff --git a/Sorts/FlashSort.js b/Sorts/FlashSort.js index 3bd1381f4..292fc2cc7 100644 --- a/Sorts/FlashSort.js +++ b/Sorts/FlashSort.js @@ -6,7 +6,7 @@ * Wikipedia: https://en.wikipedia.org/wiki/Flashsort */ -function flashSort (arr) { +export function flashSort (arr) { let max = 0; let min = arr[0] const n = arr.length const m = ~~(0.45 * n) @@ -80,11 +80,5 @@ function flashSort (arr) { /** * Implementation of Flash Sort */ -const array = [3, 0, 2, 5, -1, 4, 1, -2] -// Before Sort -console.log('\n- Before Sort | Implementation of Flash Sort -') -console.log(array) -// After Sort -console.log('- After Sort | Implementation of Flash Sort -') -console.log(flashSort(array)) -console.log('\n') +// const array = [3, 0, 2, 5, -1, 4, 1, -2] +// flashSort(array) diff --git a/Sorts/GnomeSort.js b/Sorts/GnomeSort.js index 64434d5f8..86f7219f1 100644 --- a/Sorts/GnomeSort.js +++ b/Sorts/GnomeSort.js @@ -3,7 +3,7 @@ * more information: https://en.wikipedia.org/wiki/Gnome_sort * */ -function gnomeSort (items) { +export function gnomeSort (items) { if (items.length <= 1) { return } @@ -23,9 +23,6 @@ function gnomeSort (items) { // Implementation of gnomeSort -const ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(ar) -gnomeSort(ar) -// Array after sort -console.log(ar) +// const ar = [5, 6, 7, 8, 1, 2, 12, 14] +// gnomeSort(ar) + diff --git a/Sorts/HeapSort.js b/Sorts/HeapSort.js index 1f41c8ab0..a5d78c34d 100644 --- a/Sorts/HeapSort.js +++ b/Sorts/HeapSort.js @@ -33,7 +33,7 @@ Array.prototype.heapify = function (index, heapSize) { * utilizing the heap property. * For more information see: https://en.wikipedia.org/wiki/Heapsort */ -function heapSort (items) { +export function heapSort (items) { const length = items.length for (let i = Math.floor(length / 2) - 1; i > -1; i--) { @@ -50,9 +50,5 @@ function heapSort (items) { // Implementation of heapSort -const ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(ar) -heapSort(ar) -// Array after sort -console.log(ar) +// const ar = [5, 6, 7, 8, 1, 2, 12, 14] +// heapSort(ar) diff --git a/Sorts/HeapSortV2.js b/Sorts/HeapSortV2.js index 6f010eea4..8f0d91ace 100644 --- a/Sorts/HeapSortV2.js +++ b/Sorts/HeapSortV2.js @@ -25,7 +25,7 @@ function swap (input, indexA, indexB) { [input[indexA], input[indexB]] = [input[indexB], input[indexA]] } -function heapSort (input) { +export function heapSort (input) { arrayLength = input.length for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) { @@ -39,7 +39,3 @@ function heapSort (input) { heapRoot(input, 0) } } - -const arr = [3, 0, 2, 5, -1, 4, 1] -heapSort(arr) -console.log(arr) diff --git a/Sorts/InsertionSort.js b/Sorts/InsertionSort.js index 5812acf81..a6f44dd93 100644 --- a/Sorts/InsertionSort.js +++ b/Sorts/InsertionSort.js @@ -4,7 +4,7 @@ * element one by one from unsorted part; insert into the sorted part at * the correct position and expand sorted part one element at a time. */ -function insertionSort (unsortedList) { +export function insertionSort (unsortedList) { const len = unsortedList.length for (let i = 1; i < len; i++) { let j @@ -19,7 +19,3 @@ function insertionSort (unsortedList) { unsortedList[j + 1] = tmp } } - -const arr = [5, 3, 1, 2, 4, 8, 3, 8] -insertionSort(arr) -console.log(arr) diff --git a/Sorts/IntroSort.js b/Sorts/IntroSort.js index 01d3c449d..a99c4db92 100644 --- a/Sorts/IntroSort.js +++ b/Sorts/IntroSort.js @@ -244,10 +244,10 @@ function introsort (array, compare) { /** * @example Demo run of the sort routine * The data is randomly generated - * Prints RIGHT:) if the sort routine worked as expected - * If not prints WRONG!! + * Returns 'RIGHT:)' if the sort routine worked as expected, + * 'WRONG!!' otherwise */ -(function demo () { +function demo1 () { const data = [] const size = 1000000 let i = 0 @@ -268,18 +268,18 @@ function introsort (array, compare) { } } if (faulty) { - console.log('WRONG!!') + return 'WRONG!!' } else { - console.log('RIGHT:)') + return 'RIGHT:)' } -})(); +} /** * @example Demo run of the sort routine * using the default compare function and * comparing the results with Array.sort */ -(function demo () { +function demo2 () { const data = [] const data2 = [] const size = 1000000 @@ -300,8 +300,10 @@ function introsort (array, compare) { } } if (faulty) { - console.log('WRONG Implented Comparator!!') + return 'WRONG Implented Comparator!!' } else { - console.log('Comparator Works Fine:)') + return 'Comparator Works Fine:)' } -})() +} + +export { introsort, demo1, demo2 } diff --git a/Sorts/OddEvenSort.js b/Sorts/OddEvenSort.js index ade4c7360..ee4950f4a 100644 --- a/Sorts/OddEvenSort.js +++ b/Sorts/OddEvenSort.js @@ -13,7 +13,7 @@ function swap (arr, i, j) { arr[j] = tmp } -function oddEvenSort (arr) { +export function oddEvenSort (arr) { let sorted = false while (!sorted) { sorted = true @@ -31,10 +31,3 @@ function oddEvenSort (arr) { } } } -const testArray = [5, 6, 7, 8, 1, 2, 12, 14, 5, 3, 2, 2] - -// Array before sort -console.log(testArray) -oddEvenSort(testArray) -// Array after sort -console.log(testArray) diff --git a/Sorts/PigeonHoleSort.js b/Sorts/PigeonHoleSort.js index fc2265049..333027c68 100644 --- a/Sorts/PigeonHoleSort.js +++ b/Sorts/PigeonHoleSort.js @@ -6,7 +6,7 @@ https://en.wikipedia.org/wiki/Pigeonhole_sort * (n) and the length of the range of possible key values (N) * are approximately the same. */ -function pigeonHoleSort (arr) { +export function pigeonHoleSort (arr) { let min = arr[0] let max = arr[0] @@ -14,8 +14,6 @@ function pigeonHoleSort (arr) { if (arr[i] > max) { max = arr[i] } if (arr[i] < min) { min = arr[i] } } - console.log(max) - console.log(min) const range = max - min + 1 const pigeonhole = Array(range).fill(0) @@ -32,7 +30,3 @@ function pigeonHoleSort (arr) { } } } -// Driver code -const arr = [8, 3, 2, 7, 4, 6, 8] -pigeonHoleSort(arr) -console.log(arr) diff --git a/Sorts/RadixSort.js b/Sorts/RadixSort.js index 3696aba96..a49f3c922 100644 --- a/Sorts/RadixSort.js +++ b/Sorts/RadixSort.js @@ -4,7 +4,7 @@ * significant position. * For more information see: https://en.wikipedia.org/wiki/Radix_sort */ -function radixSort (items, RADIX) { +export function radixSort (items, RADIX) { // default radix is then because we usually count to base 10 if (RADIX === undefined || RADIX < 1) { RADIX = 10 @@ -41,12 +41,3 @@ function radixSort (items, RADIX) { } return items } - -// Implementation of radixSort - -const ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(ar) -radixSort(ar) -// Array after sort -console.log(ar) diff --git a/Sorts/SelectionSort.js b/Sorts/SelectionSort.js index c4a11e354..cbb038fc3 100644 --- a/Sorts/SelectionSort.js +++ b/Sorts/SelectionSort.js @@ -8,7 +8,7 @@ *from the unsorted subarray is picked and moved to the sorted subarray. */ -const selectionSort = (list) => { +export const selectionSort = (list) => { if (!Array.isArray(list)) { throw new TypeError('Given input is not an array') } @@ -33,18 +33,3 @@ const selectionSort = (list) => { } return items } - -/* Implementation of Selection Sort - -(() => { - let array = [5, 6, 7, 8, 1, 2, 12, 14] - // Array before Sort - console.log(array) - array = selectionSort(array) - // Array after sort - console.log(array) -})() - -*/ - -export { selectionSort } diff --git a/Sorts/ShellSort.js b/Sorts/ShellSort.js index e32af561c..b2286bb10 100644 --- a/Sorts/ShellSort.js +++ b/Sorts/ShellSort.js @@ -3,7 +3,7 @@ * more information: https://en.wikipedia.org/wiki/Shellsort * */ -function shellSort (items) { +export function shellSort (items) { let interval = 1 while (interval < items.length / 3) { @@ -25,12 +25,3 @@ function shellSort (items) { } return items } - -// Implementation of shellSort - -const ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(ar) -shellSort(ar) -// Array after sort -console.log(ar) diff --git a/Sorts/TimSort.js b/Sorts/TimSort.js index 07556404b..aaeef415f 100644 --- a/Sorts/TimSort.js +++ b/Sorts/TimSort.js @@ -85,10 +85,10 @@ const Merge = (array, left, mid, right) => { /** * @example Test of Timsort functions. * Data is randomly generated. - * Prints "RIGHT" if it works as expected, + * Return "RIGHT" if it works as expected, * otherwise "FAULTY" */ -(() => { +const demo = () => { const size = 1000000 const data = Array(size) for (let i = 0; i < size; i++) { @@ -103,8 +103,10 @@ const Merge = (array, left, mid, right) => { } Timsort(data) if (isSorted(data)) { - console.log('RIGHT') + return 'RIGHT' } else { - console.log('FAULTY') + return 'FAULTY' } -})() +} + +export { Timsort, demo } diff --git a/Sorts/TopologicalSort.js b/Sorts/TopologicalSort.js index c9c5dc5e0..7bbc032c4 100644 --- a/Sorts/TopologicalSort.js +++ b/Sorts/TopologicalSort.js @@ -1,5 +1,5 @@ -function TopologicalSorter () { +export function TopologicalSorter () { const graph = {} let isVisitedNode let finishTimeCount @@ -49,11 +49,11 @@ function TopologicalSorter () { } /* TEST */ -const topoSorter = new TopologicalSorter() -topoSorter.addOrder(5, 2) -topoSorter.addOrder(5, 0) -topoSorter.addOrder(4, 0) -topoSorter.addOrder(4, 1) -topoSorter.addOrder(2, 3) -topoSorter.addOrder(3, 1) -console.log(topoSorter.sortAndGetOrderedItems()) +// const topoSorter = new TopologicalSorter() +// topoSorter.addOrder(5, 2) +// topoSorter.addOrder(5, 0) +// topoSorter.addOrder(4, 0) +// topoSorter.addOrder(4, 1) +// topoSorter.addOrder(2, 3) +// topoSorter.addOrder(3, 1) +// topoSorter.sortAndGetOrderedItems() diff --git a/Sorts/WiggleSort.js b/Sorts/WiggleSort.js index 4c303c672..aa19d596e 100644 --- a/Sorts/WiggleSort.js +++ b/Sorts/WiggleSort.js @@ -4,24 +4,18 @@ * */ -/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */ -Array.prototype.wiggleSort = function () { - for (let i = 0; i < this.length; ++i) { +export const wiggleSort = function (arr) { + for (let i = 0; i < arr.length; ++i) { const shouldNotBeLessThan = i % 2 - const isLessThan = this[i] < this[i + 1] + const isLessThan = arr[i] < arr[i + 1] if (shouldNotBeLessThan && isLessThan) { - [this[i], this[i + 1]] = [this[i + 1], this[i]] + [arr[i], arr[i + 1]] = [arr[i + 1], arr[i]] } } - return this + return arr } // Implementation of wiggle sort -const arr = [3, 5, 2, 1, 6, 4] -// Array before Wiggle Sort -console.log(arr) // [3, 5, 2, 1, 6, 4] - -arr.wiggleSort() -// Array after wiggle sort -console.log(arr) // [ 3, 5, 2, 6, 1, 4 ] +// > wiggleSort([3, 5, 2, 1, 6, 4]) +// [ 3, 5, 2, 6, 1, 4 ]