Search/Sorts algoruthms : remove live code & console.log, leave examples as comments.

This commit is contained in:
Eric Lavault
2021-10-11 12:29:03 +02:00
parent 8a7be96c9d
commit 74f296578a
22 changed files with 121 additions and 197 deletions

View File

@ -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')

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 ]

View File

@ -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')

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 }

View File

@ -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)

View File

@ -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)

View File

@ -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)

View File

@ -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 }

View File

@ -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)

View File

@ -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 }

View File

@ -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()

View File

@ -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 ]