mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-06 01:18:23 +08:00
Search/Sorts algoruthms : remove live code & console.log, leave examples as comments.
This commit is contained in:
@ -49,42 +49,43 @@ function binarySearchIterative (arr, x, low = 0, high = arr.length - 1) {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export { binarySearchIterative, binarySearchRecursive}
|
||||||
|
|
||||||
/* ---------------------------------- Test ---------------------------------- */
|
/* ---------------------------------- Test ---------------------------------- */
|
||||||
|
|
||||||
const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
// const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
|
||||||
const stringArr = [
|
// const stringArr = [
|
||||||
'Alpha',
|
// 'Alpha',
|
||||||
'Bravo',
|
// 'Bravo',
|
||||||
'Charlie',
|
// 'Charlie',
|
||||||
'Delta',
|
// 'Delta',
|
||||||
'Echo',
|
// 'Echo',
|
||||||
'Foxtrot',
|
// 'Foxtrot',
|
||||||
'Golf',
|
// 'Golf',
|
||||||
'Hotel',
|
// 'Hotel',
|
||||||
'India',
|
// 'India',
|
||||||
'Juliet',
|
// 'Juliet',
|
||||||
'Kilo',
|
// 'Kilo',
|
||||||
'Lima',
|
// 'Lima',
|
||||||
'Mike',
|
// 'Mike',
|
||||||
'November',
|
// 'November',
|
||||||
'Oscar',
|
// 'Oscar',
|
||||||
'Papa',
|
// 'Papa',
|
||||||
'Quebec',
|
// 'Quebec',
|
||||||
'Romeo',
|
// 'Romeo',
|
||||||
'Sierra',
|
// 'Sierra',
|
||||||
'Tango',
|
// 'Tango',
|
||||||
'Uniform',
|
// 'Uniform',
|
||||||
'Victor',
|
// 'Victor',
|
||||||
'Whiskey',
|
// 'Whiskey',
|
||||||
'X-Ray',
|
// 'X-Ray',
|
||||||
'Yankee',
|
// 'Yankee',
|
||||||
'Zulu'
|
// 'Zulu'
|
||||||
]
|
// ]
|
||||||
|
|
||||||
console.log(binarySearchRecursive(arr, 3))
|
// binarySearchRecursive(arr, 3)
|
||||||
console.log(binarySearchIterative(arr, 7))
|
// binarySearchIterative(arr, 7)
|
||||||
console.log(binarySearchRecursive(arr, 13))
|
// binarySearchRecursive(arr, 13)
|
||||||
|
// binarySearchIterative(stringArr, 'Charlie')
|
||||||
console.log(binarySearchIterative(stringArr, 'Charlie'))
|
// binarySearchRecursive(stringArr, 'Zulu')
|
||||||
console.log(binarySearchRecursive(stringArr, 'Zulu'))
|
// binarySearchIterative(stringArr, 'Sierra')
|
||||||
console.log(binarySearchIterative(stringArr, 'Sierra'))
|
|
||||||
|
@ -47,12 +47,8 @@ function exponentialSearch (arr, length, value) {
|
|||||||
return binarySearch(arr, value, i / 2, Math.min(i, length))
|
return binarySearch(arr, value, i / 2, Math.min(i, length))
|
||||||
}
|
}
|
||||||
|
|
||||||
const arr = [2, 3, 4, 10, 40, 65, 78, 100]
|
export { binarySearch, exponentialSearch}
|
||||||
const value = 78
|
|
||||||
const result = exponentialSearch(arr, arr.length, value)
|
|
||||||
|
|
||||||
if (result < 0) {
|
// const arr = [2, 3, 4, 10, 40, 65, 78, 100]
|
||||||
console.log('Element not found')
|
// const value = 78
|
||||||
} else {
|
// const result = exponentialSearch(arr, arr.length, value)
|
||||||
console.log('Element found at position :' + result)
|
|
||||||
}
|
|
||||||
|
@ -19,7 +19,7 @@
|
|||||||
* the item (number) to be searched for and the length of the items in the array
|
* 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 fib2 = 0 // (K-2)'th Fibonacci Number
|
||||||
let fib1 = 1 // (K-1)'th Fibonacci Number.
|
let fib1 = 1 // (K-1)'th Fibonacci Number.
|
||||||
let fibK = fib2 + fib1 // Kth Fibonacci
|
let fibK = fib2 + fib1 // Kth Fibonacci
|
||||||
@ -69,9 +69,9 @@ const fibonacciSearch = (arr, x, n) => {
|
|||||||
// element not found. return -1
|
// element not found. return -1
|
||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
// Example
|
// Example
|
||||||
const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
|
// const myArray = [10, 22, 35, 40, 45, 50, 80, 82, 85, 90, 100]
|
||||||
const n = myArray.length
|
// const n = myArray.length
|
||||||
const x = 90
|
// const x = 90
|
||||||
const fibFinder = fibonacciSearch(myArray, x, n)
|
// const fibFinder = fibonacciSearch(myArray, x, n)
|
||||||
console.log('Element found at index:', fibFinder)
|
|
||||||
|
@ -9,7 +9,7 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function interpolationSearch (arr, key) {
|
export function interpolationSearch (arr, key) {
|
||||||
const length = arr.length - 1
|
const length = arr.length - 1
|
||||||
let low = 0
|
let low = 0
|
||||||
let high = length
|
let high = length
|
||||||
@ -38,9 +38,9 @@ function interpolationSearch (arr, key) {
|
|||||||
return -1
|
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))
|
// interpolationSearch(arr, 2)
|
||||||
console.log('Found at position :' + interpolationSearch(arr, 12))
|
// interpolationSearch(arr, 12)
|
||||||
console.log('Found at position :' + interpolationSearch(arr, 1000))
|
// interpolationSearch(arr, 1000)
|
||||||
console.log('Found at position :' + interpolationSearch(arr, 39))
|
// interpolationSearch(arr, 39)
|
||||||
|
@ -21,7 +21,9 @@ function Search (theArray, key) {
|
|||||||
return -1
|
return -1
|
||||||
}
|
}
|
||||||
|
|
||||||
const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
export { SearchArray, Search }
|
||||||
SearchArray(3, ar)
|
|
||||||
SearchArray(4, ar)
|
// const ar = [1, 2, 3, 4, 5, 6, 7, 8, 9]
|
||||||
SearchArray(11, ar)
|
// SearchArray(3, ar)
|
||||||
|
// SearchArray(4, ar)
|
||||||
|
// SearchArray(11, ar)
|
||||||
|
@ -11,7 +11,7 @@
|
|||||||
*
|
*
|
||||||
* [Reference](http://en.wikipedia.org/wiki/Quickselect)
|
* [Reference](http://en.wikipedia.org/wiki/Quickselect)
|
||||||
*/
|
*/
|
||||||
function quickSelectSearch (array, k) {
|
export function quickSelectSearch (array, k) {
|
||||||
if (!array || array.length <= k) {
|
if (!array || array.length <= k) {
|
||||||
throw new Error('Invalid arguments')
|
throw new Error('Invalid arguments')
|
||||||
}
|
}
|
||||||
@ -49,7 +49,7 @@ function quickSelectSearch (array, k) {
|
|||||||
|
|
||||||
/* ---------------------------------- Test ---------------------------------- */
|
/* ---------------------------------- Test ---------------------------------- */
|
||||||
|
|
||||||
const arr = [1121111, 21, 333, 41, 5, 66, 7777, 28, 19, 11110]
|
// 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 ]
|
// 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 ]
|
// 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 ]
|
// quickSelectSearch(arr, 7) // [ 19, 5, 21, 41, 28, 66, 333, 7777, 11110, 1121111 ]
|
||||||
|
@ -35,7 +35,7 @@ function makeTable (str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Find all the words that matches in a given string `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)
|
// find the prefix table in O(n)
|
||||||
const prefixes = makeTable(word)
|
const prefixes = makeTable(word)
|
||||||
const matches = []
|
const matches = []
|
||||||
@ -80,4 +80,4 @@ function stringSearch (str, word) {
|
|||||||
return matches
|
return matches
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(stringSearch('Hello search the position of me', 'pos'))
|
// stringSearch('Hello search the position of me', 'pos')
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
* Animated Visual: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html
|
* 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
|
// Create an auxiliary resultant array
|
||||||
const res = []
|
const res = []
|
||||||
// Create and initialize the frequency[count] array
|
// Create and initialize the frequency[count] array
|
||||||
@ -33,11 +33,5 @@ const countingSort = (arr, min, max) => {
|
|||||||
/**
|
/**
|
||||||
* Implementation of Counting Sort
|
* Implementation of Counting Sort
|
||||||
*/
|
*/
|
||||||
const array = [3, 0, 2, 5, 4, 1]
|
// const array = [3, 0, 2, 5, 4, 1]
|
||||||
// Before Sort
|
// countingSort(array, 0, 5)
|
||||||
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')
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
* Wikipedia: https://en.wikipedia.org/wiki/Flashsort
|
* Wikipedia: https://en.wikipedia.org/wiki/Flashsort
|
||||||
*/
|
*/
|
||||||
|
|
||||||
function flashSort (arr) {
|
export function flashSort (arr) {
|
||||||
let max = 0; let min = arr[0]
|
let max = 0; let min = arr[0]
|
||||||
const n = arr.length
|
const n = arr.length
|
||||||
const m = ~~(0.45 * n)
|
const m = ~~(0.45 * n)
|
||||||
@ -80,11 +80,5 @@ function flashSort (arr) {
|
|||||||
/**
|
/**
|
||||||
* Implementation of Flash Sort
|
* Implementation of Flash Sort
|
||||||
*/
|
*/
|
||||||
const array = [3, 0, 2, 5, -1, 4, 1, -2]
|
// const array = [3, 0, 2, 5, -1, 4, 1, -2]
|
||||||
// Before Sort
|
// flashSort(array)
|
||||||
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')
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
* more information: https://en.wikipedia.org/wiki/Gnome_sort
|
* more information: https://en.wikipedia.org/wiki/Gnome_sort
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function gnomeSort (items) {
|
export function gnomeSort (items) {
|
||||||
if (items.length <= 1) {
|
if (items.length <= 1) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -23,9 +23,6 @@ function gnomeSort (items) {
|
|||||||
|
|
||||||
// Implementation of gnomeSort
|
// Implementation of gnomeSort
|
||||||
|
|
||||||
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
// const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// gnomeSort(ar)
|
||||||
console.log(ar)
|
|
||||||
gnomeSort(ar)
|
|
||||||
// Array after sort
|
|
||||||
console.log(ar)
|
|
||||||
|
@ -33,7 +33,7 @@ Array.prototype.heapify = function (index, heapSize) {
|
|||||||
* utilizing the heap property.
|
* utilizing the heap property.
|
||||||
* For more information see: https://en.wikipedia.org/wiki/Heapsort
|
* For more information see: https://en.wikipedia.org/wiki/Heapsort
|
||||||
*/
|
*/
|
||||||
function heapSort (items) {
|
export function heapSort (items) {
|
||||||
const length = items.length
|
const length = items.length
|
||||||
|
|
||||||
for (let i = Math.floor(length / 2) - 1; i > -1; i--) {
|
for (let i = Math.floor(length / 2) - 1; i > -1; i--) {
|
||||||
@ -50,9 +50,5 @@ function heapSort (items) {
|
|||||||
|
|
||||||
// Implementation of heapSort
|
// Implementation of heapSort
|
||||||
|
|
||||||
const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
// const ar = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
// Array before Sort
|
// heapSort(ar)
|
||||||
console.log(ar)
|
|
||||||
heapSort(ar)
|
|
||||||
// Array after sort
|
|
||||||
console.log(ar)
|
|
||||||
|
@ -25,7 +25,7 @@ function swap (input, indexA, indexB) {
|
|||||||
[input[indexA], input[indexB]] = [input[indexB], input[indexA]]
|
[input[indexA], input[indexB]] = [input[indexB], input[indexA]]
|
||||||
}
|
}
|
||||||
|
|
||||||
function heapSort (input) {
|
export function heapSort (input) {
|
||||||
arrayLength = input.length
|
arrayLength = input.length
|
||||||
|
|
||||||
for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) {
|
for (let i = Math.floor(arrayLength / 2); i >= 0; i -= 1) {
|
||||||
@ -39,7 +39,3 @@ function heapSort (input) {
|
|||||||
heapRoot(input, 0)
|
heapRoot(input, 0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const arr = [3, 0, 2, 5, -1, 4, 1]
|
|
||||||
heapSort(arr)
|
|
||||||
console.log(arr)
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* element one by one from unsorted part; insert into the sorted part at
|
* 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.
|
* the correct position and expand sorted part one element at a time.
|
||||||
*/
|
*/
|
||||||
function insertionSort (unsortedList) {
|
export function insertionSort (unsortedList) {
|
||||||
const len = unsortedList.length
|
const len = unsortedList.length
|
||||||
for (let i = 1; i < len; i++) {
|
for (let i = 1; i < len; i++) {
|
||||||
let j
|
let j
|
||||||
@ -19,7 +19,3 @@ function insertionSort (unsortedList) {
|
|||||||
unsortedList[j + 1] = tmp
|
unsortedList[j + 1] = tmp
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const arr = [5, 3, 1, 2, 4, 8, 3, 8]
|
|
||||||
insertionSort(arr)
|
|
||||||
console.log(arr)
|
|
||||||
|
@ -244,10 +244,10 @@ function introsort (array, compare) {
|
|||||||
/**
|
/**
|
||||||
* @example Demo run of the sort routine
|
* @example Demo run of the sort routine
|
||||||
* The data is randomly generated
|
* The data is randomly generated
|
||||||
* Prints RIGHT:) if the sort routine worked as expected
|
* Returns 'RIGHT:)' if the sort routine worked as expected,
|
||||||
* If not prints WRONG!!
|
* 'WRONG!!' otherwise
|
||||||
*/
|
*/
|
||||||
(function demo () {
|
function demo1 () {
|
||||||
const data = []
|
const data = []
|
||||||
const size = 1000000
|
const size = 1000000
|
||||||
let i = 0
|
let i = 0
|
||||||
@ -268,18 +268,18 @@ function introsort (array, compare) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (faulty) {
|
if (faulty) {
|
||||||
console.log('WRONG!!')
|
return 'WRONG!!'
|
||||||
} else {
|
} else {
|
||||||
console.log('RIGHT:)')
|
return 'RIGHT:)'
|
||||||
}
|
}
|
||||||
})();
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @example Demo run of the sort routine
|
* @example Demo run of the sort routine
|
||||||
* using the default compare function and
|
* using the default compare function and
|
||||||
* comparing the results with Array.sort
|
* comparing the results with Array.sort
|
||||||
*/
|
*/
|
||||||
(function demo () {
|
function demo2 () {
|
||||||
const data = []
|
const data = []
|
||||||
const data2 = []
|
const data2 = []
|
||||||
const size = 1000000
|
const size = 1000000
|
||||||
@ -300,8 +300,10 @@ function introsort (array, compare) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (faulty) {
|
if (faulty) {
|
||||||
console.log('WRONG Implented Comparator!!')
|
return 'WRONG Implented Comparator!!'
|
||||||
} else {
|
} else {
|
||||||
console.log('Comparator Works Fine:)')
|
return 'Comparator Works Fine:)'
|
||||||
}
|
}
|
||||||
})()
|
}
|
||||||
|
|
||||||
|
export { introsort, demo1, demo2 }
|
||||||
|
@ -13,7 +13,7 @@ function swap (arr, i, j) {
|
|||||||
arr[j] = tmp
|
arr[j] = tmp
|
||||||
}
|
}
|
||||||
|
|
||||||
function oddEvenSort (arr) {
|
export function oddEvenSort (arr) {
|
||||||
let sorted = false
|
let sorted = false
|
||||||
while (!sorted) {
|
while (!sorted) {
|
||||||
sorted = true
|
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)
|
|
||||||
|
@ -6,7 +6,7 @@ https://en.wikipedia.org/wiki/Pigeonhole_sort
|
|||||||
* (n) and the length of the range of possible key values (N)
|
* (n) and the length of the range of possible key values (N)
|
||||||
* are approximately the same.
|
* are approximately the same.
|
||||||
*/
|
*/
|
||||||
function pigeonHoleSort (arr) {
|
export function pigeonHoleSort (arr) {
|
||||||
let min = arr[0]
|
let min = arr[0]
|
||||||
let max = arr[0]
|
let max = arr[0]
|
||||||
|
|
||||||
@ -14,8 +14,6 @@ function pigeonHoleSort (arr) {
|
|||||||
if (arr[i] > max) { max = arr[i] }
|
if (arr[i] > max) { max = arr[i] }
|
||||||
if (arr[i] < min) { min = arr[i] }
|
if (arr[i] < min) { min = arr[i] }
|
||||||
}
|
}
|
||||||
console.log(max)
|
|
||||||
console.log(min)
|
|
||||||
|
|
||||||
const range = max - min + 1
|
const range = max - min + 1
|
||||||
const pigeonhole = Array(range).fill(0)
|
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)
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
* significant position.
|
* significant position.
|
||||||
* For more information see: https://en.wikipedia.org/wiki/Radix_sort
|
* 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
|
// default radix is then because we usually count to base 10
|
||||||
if (RADIX === undefined || RADIX < 1) {
|
if (RADIX === undefined || RADIX < 1) {
|
||||||
RADIX = 10
|
RADIX = 10
|
||||||
@ -41,12 +41,3 @@ function radixSort (items, RADIX) {
|
|||||||
}
|
}
|
||||||
return items
|
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)
|
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
*from the unsorted subarray is picked and moved to the sorted subarray.
|
*from the unsorted subarray is picked and moved to the sorted subarray.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
const selectionSort = (list) => {
|
export const selectionSort = (list) => {
|
||||||
if (!Array.isArray(list)) {
|
if (!Array.isArray(list)) {
|
||||||
throw new TypeError('Given input is not an array')
|
throw new TypeError('Given input is not an array')
|
||||||
}
|
}
|
||||||
@ -33,18 +33,3 @@ const selectionSort = (list) => {
|
|||||||
}
|
}
|
||||||
return items
|
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 }
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
* more information: https://en.wikipedia.org/wiki/Shellsort
|
* more information: https://en.wikipedia.org/wiki/Shellsort
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function shellSort (items) {
|
export function shellSort (items) {
|
||||||
let interval = 1
|
let interval = 1
|
||||||
|
|
||||||
while (interval < items.length / 3) {
|
while (interval < items.length / 3) {
|
||||||
@ -25,12 +25,3 @@ function shellSort (items) {
|
|||||||
}
|
}
|
||||||
return 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)
|
|
||||||
|
@ -85,10 +85,10 @@ const Merge = (array, left, mid, right) => {
|
|||||||
/**
|
/**
|
||||||
* @example Test of Timsort functions.
|
* @example Test of Timsort functions.
|
||||||
* Data is randomly generated.
|
* Data is randomly generated.
|
||||||
* Prints "RIGHT" if it works as expected,
|
* Return "RIGHT" if it works as expected,
|
||||||
* otherwise "FAULTY"
|
* otherwise "FAULTY"
|
||||||
*/
|
*/
|
||||||
(() => {
|
const demo = () => {
|
||||||
const size = 1000000
|
const size = 1000000
|
||||||
const data = Array(size)
|
const data = Array(size)
|
||||||
for (let i = 0; i < size; i++) {
|
for (let i = 0; i < size; i++) {
|
||||||
@ -103,8 +103,10 @@ const Merge = (array, left, mid, right) => {
|
|||||||
}
|
}
|
||||||
Timsort(data)
|
Timsort(data)
|
||||||
if (isSorted(data)) {
|
if (isSorted(data)) {
|
||||||
console.log('RIGHT')
|
return 'RIGHT'
|
||||||
} else {
|
} else {
|
||||||
console.log('FAULTY')
|
return 'FAULTY'
|
||||||
}
|
}
|
||||||
})()
|
}
|
||||||
|
|
||||||
|
export { Timsort, demo }
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
|
|
||||||
function TopologicalSorter () {
|
export function TopologicalSorter () {
|
||||||
const graph = {}
|
const graph = {}
|
||||||
let isVisitedNode
|
let isVisitedNode
|
||||||
let finishTimeCount
|
let finishTimeCount
|
||||||
@ -49,11 +49,11 @@ function TopologicalSorter () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* TEST */
|
/* TEST */
|
||||||
const topoSorter = new TopologicalSorter()
|
// const topoSorter = new TopologicalSorter()
|
||||||
topoSorter.addOrder(5, 2)
|
// topoSorter.addOrder(5, 2)
|
||||||
topoSorter.addOrder(5, 0)
|
// topoSorter.addOrder(5, 0)
|
||||||
topoSorter.addOrder(4, 0)
|
// topoSorter.addOrder(4, 0)
|
||||||
topoSorter.addOrder(4, 1)
|
// topoSorter.addOrder(4, 1)
|
||||||
topoSorter.addOrder(2, 3)
|
// topoSorter.addOrder(2, 3)
|
||||||
topoSorter.addOrder(3, 1)
|
// topoSorter.addOrder(3, 1)
|
||||||
console.log(topoSorter.sortAndGetOrderedItems())
|
// topoSorter.sortAndGetOrderedItems()
|
||||||
|
@ -4,24 +4,18 @@
|
|||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
|
export const wiggleSort = function (arr) {
|
||||||
Array.prototype.wiggleSort = function () {
|
for (let i = 0; i < arr.length; ++i) {
|
||||||
for (let i = 0; i < this.length; ++i) {
|
|
||||||
const shouldNotBeLessThan = i % 2
|
const shouldNotBeLessThan = i % 2
|
||||||
const isLessThan = this[i] < this[i + 1]
|
const isLessThan = arr[i] < arr[i + 1]
|
||||||
if (shouldNotBeLessThan && isLessThan) {
|
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
|
// Implementation of wiggle sort
|
||||||
|
|
||||||
const arr = [3, 5, 2, 1, 6, 4]
|
// > wiggleSort([3, 5, 2, 1, 6, 4])
|
||||||
// Array before Wiggle Sort
|
// [ 3, 5, 2, 6, 1, 4 ]
|
||||||
console.log(arr) // [3, 5, 2, 1, 6, 4]
|
|
||||||
|
|
||||||
arr.wiggleSort()
|
|
||||||
// Array after wiggle sort
|
|
||||||
console.log(arr) // [ 3, 5, 2, 6, 1, 4 ]
|
|
||||||
|
Reference in New Issue
Block a user