chore: Merge pull request #768 from lvlte/issues/720

Changes for consolidation
This commit is contained in:
Rak Laptudirm
2021-10-21 19:32:55 +05:30
committed by GitHub
153 changed files with 1084 additions and 1214 deletions

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