npx standard --fix

This commit is contained in:
cclauss
2020-05-03 09:05:12 +02:00
parent e62ad2f73e
commit 856dc2f63c
47 changed files with 2240 additions and 2371 deletions

View File

@@ -1,59 +1,58 @@
function TopologicalSorter() {
var graph = {},
isVisitedNode,
finishTimeCount,
finishingTimeList,
nextNode;
function TopologicalSorter () {
var graph = {}
var isVisitedNode
var finishTimeCount
var finishingTimeList
var nextNode
this.addOrder = function (nodeA, nodeB) {
nodeA = String(nodeA);
nodeB = String(nodeB);
graph[nodeA] = graph[nodeA] || [];
graph[nodeA].push(nodeB);
}
this.sortAndGetOrderedItems = function () {
isVisitedNode = Object.create(null);
finishTimeCount = 0;
finishingTimeList = [];
this.addOrder = function (nodeA, nodeB) {
nodeA = String(nodeA)
nodeB = String(nodeB)
graph[nodeA] = graph[nodeA] || []
graph[nodeA].push(nodeB)
}
for (var node in graph) {
if (graph.hasOwnProperty(node) && !isVisitedNode[node]) {
dfsTraverse(node);
}
}
this.sortAndGetOrderedItems = function () {
isVisitedNode = Object.create(null)
finishTimeCount = 0
finishingTimeList = []
finishingTimeList.sort(function (item1, item2) {
return item1.finishTime > item2.finishTime ? -1 : 1;
});
return finishingTimeList.map(function (value) { return value.node })
for (var node in graph) {
if (graph.hasOwnProperty(node) && !isVisitedNode[node]) {
dfsTraverse(node)
}
}
function dfsTraverse(node) {
isVisitedNode[node] = true;
if (graph[node]) {
for (var i = 0; i < graph[node].length; i++) {
nextNode = graph[node][i];
if (isVisitedNode[nextNode]) continue;
dfsTraverse(nextNode);
}
}
finishingTimeList.sort(function (item1, item2) {
return item1.finishTime > item2.finishTime ? -1 : 1
})
finishingTimeList.push({
node: node,
finishTime: ++finishTimeCount
});
return finishingTimeList.map(function (value) { return value.node })
}
function dfsTraverse (node) {
isVisitedNode[node] = true
if (graph[node]) {
for (var i = 0; i < graph[node].length; i++) {
nextNode = graph[node][i]
if (isVisitedNode[nextNode]) continue
dfsTraverse(nextNode)
}
}
finishingTimeList.push({
node: node,
finishTime: ++finishTimeCount
})
}
}
/* TEST */
var 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());
var 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())

View File

@@ -3,53 +3,49 @@
* sorted in ascending order.
*/
Array.prototype.isSorted = function () {
const length = this.length
let length = this.length;
if (length < 2) {
return true
}
if (length < 2) {
return true;
for (let i = 0; i < length - 1; i++) {
if (this[i] > this[i + 1]) {
return false
}
for (let i = 0; i < length - 1; i++) {
if (this[i] > this[i + 1]) {
return false;
}
}
return true;
};
}
return true
}
/*
* A simple helper function to shuffle the array randomly in place.
*/
Array.prototype.shuffle = function () {
for (let i = this.length - 1; i; i--) {
let m = Math.floor(Math.random() * i);
let n = this[i - 1];
this[i - 1] = this[m];
this[m] = n;
}
};
for (let i = this.length - 1; i; i--) {
const m = Math.floor(Math.random() * i)
const n = this[i - 1]
this[i - 1] = this[m]
this[m] = n
}
}
/*
* Implementation of the bogosort algorithm. This sorting algorithm randomly
* rearranges the array until it is sorted.
* For more information see: https://en.wikipedia.org/wiki/Bogosort
*/
function bogoSort(items) {
while (!items.isSorted()) {
items.shuffle()
}
return items;
function bogoSort (items) {
while (!items.isSorted()) {
items.shuffle()
}
return items
}
//Implementation of bogoSort
// Implementation of bogoSort
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(ar);
bogoSort(ar);
//Array after sort
console.log(ar);
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
bogoSort(ar)
// Array after sort
console.log(ar)

View File

@@ -1,4 +1,4 @@
/*
/*
Wikipedia says: Bucket sort, or bin sort, is a sorting algorithm that works by distributing the
elements of an array into a number of buckets. Each bucket is then sorted individually, either using
a different sorting algorithm, or by recursively applying the bucket sorting algorithm. It is a
@@ -11,52 +11,50 @@ Time Complexity of Solution:
Best Case O(n); Average Case O(n); Worst Case O(n)
*/
function bucketSort(list, size){
if(undefined === size){
size = 5;
function bucketSort (list, size) {
if (undefined === size) {
size = 5
}
if (list.length === 0) {
return list
}
let min = list[0]
let max = list[0]
// find min and max
for (let iList = 0; iList < list.length; iList++) {
if (list[iList] < min) {
min = list[iList]
} else if (list[iList] > max) {
max = list[iList]
}
if(list.length === 0){
return list;
}
let min = list[0];
let max = list[0];
// find min and max
for(let iList = 0; iList < list.length; iList++){
if(list[iList] < min){
min = list[iList];
} else if(list[iList] > max){
max = list[iList];
}
}
// how many buckets we need
let count = Math.floor((max - min) / size) + 1;
}
// how many buckets we need
const count = Math.floor((max - min) / size) + 1
// create buckets
let buckets = [];
for(let iCount = 0; iCount < count; iCount++){
buckets.push([]);
}
// create buckets
const buckets = []
for (let iCount = 0; iCount < count; iCount++) {
buckets.push([])
}
// bucket fill
for(let iBucket = 0; iBucket < list.length; iBucket++){
let key = Math.floor((list[iBucket] - min) / size);
buckets[key].push(list[iBucket]);
// bucket fill
for (let iBucket = 0; iBucket < list.length; iBucket++) {
const key = Math.floor((list[iBucket] - min) / size)
buckets[key].push(list[iBucket])
}
const sorted = []
// now sort every bucket and merge it to the sorted list
for (let iBucket = 0; iBucket < buckets.length; iBucket++) {
const arr = buckets[iBucket].sort()
for (let iSorted = 0; iSorted < arr.length; iSorted++) {
sorted.push(arr[iSorted])
}
let sorted = [];
// now sort every bucket and merge it to the sorted list
for(let iBucket = 0; iBucket < buckets.length; iBucket++){
let arr = buckets[iBucket].sort();
for(let iSorted = 0; iSorted < arr.length; iSorted++){
sorted.push(arr[iSorted]);
}
}
return sorted;
}
return sorted
}
let arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(arrOrignal);
arrSorted = bucketSort(arrOrignal);
//Array after sort
console.log(arrSorted);
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(arrOrignal)
arrSorted = bucketSort(arrOrignal)
// Array after sort
console.log(arrSorted)

View File

@@ -4,42 +4,41 @@
* more information: https://en.wikipedia.org/wiki/Bubble_sort
*
*/
function cocktailShakerSort(items) {
function cocktailShakerSort (items) {
for (let i = items.length - 1; i > 0; i--) {
let swapped = false
let temp, j
for (let i = items.length - 1; i > 0; i--) {
let swapped = false;
let temp, j;
// backwards
for (j = items.length -1; j > i; j--) {
if (items[j] < items[j - 1]) {
temp = items[j];
items[j] = items[j - 1];
items[j - 1] = temp;
swapped = true;
}
}
//forwards
for (j = 0; j < i; j++) {
if (items[j] > items[j + 1]) {
temp = items[j];
items[j] = items[j + 1];
items[j + 1] = temp;
swapped = true;
}
}
if (!swapped) {
return;
}
// backwards
for (j = items.length - 1; j > i; j--) {
if (items[j] < items[j - 1]) {
temp = items[j]
items[j] = items[j - 1]
items[j - 1] = temp
swapped = true
}
}
// forwards
for (j = 0; j < i; j++) {
if (items[j] > items[j + 1]) {
temp = items[j]
items[j] = items[j + 1]
items[j + 1] = temp
swapped = true
}
}
if (!swapped) {
return
}
}
}
//Implementation of cocktailShakerSort
// Implementation of cocktailShakerSort
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(ar);
cocktailShakerSort(ar);
//Array after sort
console.log(ar);
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
cocktailShakerSort(ar)
// Array after sort
console.log(ar)

View File

@@ -1,53 +1,50 @@
/*
Wikipedia says: Comb sort improves on bubble sort.
/*
Wikipedia says: Comb sort improves on bubble sort.
The basic idea is to eliminate turtles, or small values
near the end of the list, since in a bubble sort these slow the sorting
down tremendously. Rabbits, large values around the beginning of the list,
The basic idea is to eliminate turtles, or small values
near the end of the list, since in a bubble sort these slow the sorting
down tremendously. Rabbits, large values around the beginning of the list,
do not pose a problem in bubble sort.
In bubble sort, when any two elements are compared, they always have a
gap (distance from each other) of 1. The basic idea of comb sort is
that the gap can be much more than 1. The inner loop of bubble sort,
which does the actual swap, is modified such that gap between swapped
elements goes down (for each iteration of outer loop) in steps of
In bubble sort, when any two elements are compared, they always have a
gap (distance from each other) of 1. The basic idea of comb sort is
that the gap can be much more than 1. The inner loop of bubble sort,
which does the actual swap, is modified such that gap between swapped
elements goes down (for each iteration of outer loop) in steps of
a "shrink factor" k: [ n/k, n/k2, n/k3, ..., 1 ].
*/
function combSort(list) {
if (list.length === 0) {
return list;
}
let shrink = 1.3;
let gap = list.length;
let isSwapped = true;
let i = 0
while (gap > 1 || isSwapped) {
// Update the gap value for a next comb
gap = parseInt(parseFloat(gap) / shrink, 10);
isSwapped = false
i = 0
while (gap + i < list.length) {
if (list[i] > list[i + gap]) {
let value = list[i];
list[i] = list[i + gap];
list[i + gap] = value;
isSwapped = true;
}
i += 1
}
}
function combSort (list) {
if (list.length === 0) {
return list
}
const shrink = 1.3
let gap = list.length
let isSwapped = true
let i = 0
while (gap > 1 || isSwapped) {
// Update the gap value for a next comb
gap = parseInt(parseFloat(gap) / shrink, 10)
isSwapped = false
i = 0
while (gap + i < list.length) {
if (list[i] > list[i + gap]) {
const value = list[i]
list[i] = list[i + gap]
list[i + gap] = value
isSwapped = true
}
i += 1
}
}
return list
}
let arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(arrOrignal);
arrSorted = combSort(arrOrignal);
//Array after sort
console.log(arrSorted);
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(arrOrignal)
arrSorted = combSort(arrOrignal)
// Array after sort
console.log(arrSorted)

View File

@@ -5,33 +5,33 @@
* counting sort visualization: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html
*/
function countingSort(arr, min, max) {
let i;
let z = 0;
const count = [];
function countingSort (arr, min, max) {
let i
let z = 0
const count = []
for (i = min; i <= max; i++) {
count[i] = 0;
count[i] = 0
}
for (i = 0; i < arr.length; i++) {
count[arr[i]]++;
count[arr[i]]++
}
for (i = min; i <= max; i++) {
while (count[i]-- > 0) {
arr[z++] = i;
arr[z++] = i
}
}
return arr;
return arr
}
const arr = [3, 0, 2, 5, 4, 1];
const arr = [3, 0, 2, 5, 4, 1]
// Array before Sort
console.log("-----before sorting-----");
console.log(arr);
console.log('-----before sorting-----')
console.log(arr)
// Array after sort
console.log("-----after sorting-----");
console.log(countingSort(arr, 0, 5));
console.log('-----after sorting-----')
console.log(countingSort(arr, 0, 5))

View File

@@ -1,62 +1,58 @@
/*
Wikipedia says: Cycle sort is an in-place, unstable sorting algorithm,
a comparison sort that is theoretically optimal in terms of the total
number of writes to the original array, unlike any other in-place sorting
algorithm. It is based on the idea that the permutation to be sorted can
/*
Wikipedia says: Cycle sort is an in-place, unstable sorting algorithm,
a comparison sort that is theoretically optimal in terms of the total
number of writes to the original array, unlike any other in-place sorting
algorithm. It is based on the idea that the permutation to be sorted can
be factored into cycles, which can individually be rotated to give a sorted result.
*/
function cycleSort(list) {
function cycleSort (list) {
let writes = 0
for (let cycleStart = 0; cycleStart < list.length; cycleStart++) {
let value = list[cycleStart]
let position = cycleStart
let writes = 0;
for (let cycleStart = 0; cycleStart < list.length; cycleStart++) {
let value = list[cycleStart];
let position = cycleStart;
// search position
for (let i = cycleStart+1; i < list.length; i++) {
if (list[i] < value) {
position++;
}
}
// if its the same continue
if (position == cycleStart) {
continue;
}
while (value == list[position]) {
position++;
}
let oldValue = list[position];
list[position] = value;
value = oldValue;
writes++;
// rotate the rest
while (position != cycleStart) {
position = cycleStart;
for (let i = cycleStart +1; i < list.length; i++) {
if (list[i] < value) {
position++;
}
}
while (value == list[position]) {
position++;
}
let oldValueCycle = list[position];
list[position] = value;
value = oldValueCycle;
writes++;
}
// search position
for (let i = cycleStart + 1; i < list.length; i++) {
if (list[i] < value) {
position++
}
}
return writes;
// if its the same continue
if (position == cycleStart) {
continue
}
while (value == list[position]) {
position++
}
const oldValue = list[position]
list[position] = value
value = oldValue
writes++
// rotate the rest
while (position != cycleStart) {
position = cycleStart
for (let i = cycleStart + 1; i < list.length; i++) {
if (list[i] < value) {
position++
}
}
while (value == list[position]) {
position++
}
const oldValueCycle = list[position]
list[position] = value
value = oldValueCycle
writes++
}
}
return writes
}
let arrOrignal = [5, 6, 7, 8, 1, 2,12, 14];
//Array before Sort
console.log(arrOrignal);
cycleSort(arrOrignal);
//Array after sort
console.log(arrOrignal);
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(arrOrignal)
cycleSort(arrOrignal)
// Array after sort
console.log(arrOrignal)

View File

@@ -4,82 +4,82 @@
* more information: https://en.wikipedia.org/wiki/Flashsort
*/
function flashSort(arr) {
let max = 0, min = arr[0];
let n = arr.length;
let m = ~~(0.45 * n);
let l = new Array(m);
function flashSort (arr) {
let max = 0; let min = arr[0]
const n = arr.length
const m = ~~(0.45 * n)
const l = new Array(m)
for (let i = 1; i < n; ++i) {
if (arr[i] < min) {
min = arr[i];
min = arr[i]
}
if (arr[i] > arr[max]) {
max = i;
max = i
}
}
if (min === arr[max]) {
return arr;
return arr
}
let c1 = (m - 1) / (arr[max] - min);
const c1 = (m - 1) / (arr[max] - min)
for (let k = 0; k < m; k++) {
l[k] = 0;
l[k] = 0
}
for (let j = 0; j < n; ++j) {
let k = ~~(c1 * (arr[j] - min));
++l[k];
const k = ~~(c1 * (arr[j] - min))
++l[k]
}
for (let p = 1; p < m; ++p) {
l[p] = l[p] + l[p - 1];
l[p] = l[p] + l[p - 1]
}
let hold = arr[max];
arr[max] = arr[0];
arr[0] = hold;
let hold = arr[max]
arr[max] = arr[0]
arr[0] = hold
// permutation
let move = 0, t, flash;
let j = 0;
let k = m - 1;
let move = 0; let t; let flash
let j = 0
let k = m - 1
while (move < (n - 1)) {
while (j > (l[k] - 1)) {
++j;
k = ~~(c1 * (arr[j] - min));
++j
k = ~~(c1 * (arr[j] - min))
}
if (k < 0) break;
flash = arr[j];
if (k < 0) break
flash = arr[j]
while (j !== l[k]) {
k = ~~(c1 * (flash - min));
hold = arr[t = --l[k]];
arr[t] = flash;
flash = hold;
++move;
k = ~~(c1 * (flash - min))
hold = arr[t = --l[k]]
arr[t] = flash
flash = hold
++move
}
}
// insertion
for (j = 1; j < n; j++) {
hold = arr[j];
let i = j - 1;
hold = arr[j]
let i = j - 1
while (i >= 0 && arr[i] > hold) {
arr[i + 1] = arr[i--];
arr[i + 1] = arr[i--]
}
arr[i + 1] = hold;
arr[i + 1] = hold
}
return arr;
return arr
}
const array = [3, 0, 2, 5, -1, 4, 1, -2];
const array = [3, 0, 2, 5, -1, 4, 1, -2]
// Array before Sort
console.log("-----before sorting-----");
console.log(array);
console.log('-----before sorting-----')
console.log(array)
// Array after sort
console.log("-----after sorting-----");
console.log(flashSort(array));
console.log('-----after sorting-----')
console.log(flashSort(array))

View File

@@ -3,34 +3,31 @@
* more information: https://en.wikipedia.org/wiki/Gnome_sort
*
*/
function gnomeSort(items) {
function gnomeSort (items) {
if (items.length <= 1) {
return
}
if (items.length <= 1) {
let i = 1
return;
}
let i = 1;
while (i < items.length) {
if (items[i - 1] <= items[i]) {
i++;
} else {
let temp = items[i];
items[i] = items[i - 1];
items[i - 1] = temp;
i = Math.max(1, i - 1);
}
while (i < items.length) {
if (items[i - 1] <= items[i]) {
i++
} else {
const temp = items[i]
items[i] = items[i - 1]
items[i - 1] = temp
i = Math.max(1, i - 1)
}
}
}
//Implementation of gnomeSort
// Implementation of gnomeSort
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(ar);
gnomeSort(ar);
//Array after sort
console.log(ar);
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
gnomeSort(ar)
// Array after sort
console.log(ar)

View File

@@ -6,54 +6,52 @@
* Source: https://en.wikipedia.org/wiki/Heap_(data_structure)
*/
Array.prototype.heapify = function (index, heapSize) {
let largest = index;
let leftIndex = 2 * index + 1;
let rightIndex = 2 * index + 2;
let largest = index
const leftIndex = 2 * index + 1
const rightIndex = 2 * index + 2
if (leftIndex < heapSize && this[leftIndex] > this[largest]) {
largest = leftIndex;
largest = leftIndex
}
if (rightIndex < heapSize && this[rightIndex] > this[largest]) {
largest = rightIndex;
largest = rightIndex
}
if (largest !== index) {
let temp = this[largest];
this[largest] = this[index];
this[index] = temp;
const temp = this[largest]
this[largest] = this[index]
this[index] = temp
this.heapify(largest, heapSize);
this.heapify(largest, heapSize)
}
};
}
/*
* Heap sort sorts an array by building a heap from the array and
* utilizing the heap property.
* For more information see: https://en.wikipedia.org/wiki/Heapsort
*/
function heapSort(items) {
let length = items.length;
function heapSort (items) {
const length = items.length
for (let i = Math.floor(length / 2) - 1; i > -1; i--) {
items.heapify(i, length);
items.heapify(i, length)
}
for (let j = length -1; j > 0; j--) {
let tmp = items[0];
items[0] = items[j];
items[j] = tmp;
items.heapify(0, j);
for (let j = length - 1; j > 0; j--) {
const tmp = items[0]
items[0] = items[j]
items[j] = tmp
items.heapify(0, j)
}
return items;
return items
}
//Implementation of heapSort
// Implementation of heapSort
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(ar);
heapSort(ar);
//Array after sort
console.log(ar);
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
heapSort(ar)
// Array after sort
console.log(ar)

View File

@@ -1,24 +1,24 @@
/*In insertion sort, we divide the initial unsorted array into two parts;
/* In insertion sort, we divide the initial unsorted array into two parts;
* sorted part and unsorted part. Initially the sorted part just has one
* element (Array of only 1 element is a sorted array). We then pick up
* 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) {
var len = unsortedList.length;
function insertionSort (unsortedList) {
var len = unsortedList.length
for (var i = 1; i < len; i++) {
var tmp = unsortedList[i]; //Copy of the current element.
/*Check through the sorted part and compare with the number in tmp. If large, shift the number*/
var tmp = unsortedList[i] // Copy of the current element.
/* Check through the sorted part and compare with the number in tmp. If large, shift the number */
for (var j = i - 1; j >= 0 && (unsortedList[j] > tmp); j--) {
//Shift the number
unsortedList[j + 1] = unsortedList[j];
// Shift the number
unsortedList[j + 1] = unsortedList[j]
}
//Insert the copied number at the correct position
//in sorted part.
unsortedList[j + 1] = tmp;
// Insert the copied number at the correct position
// in sorted part.
unsortedList[j + 1] = tmp
}
}
var arr = [5, 3, 1, 2, 4, 8, 3, 8];
insertionSort(arr);
console.log(arr);
var arr = [5, 3, 1, 2, 4, 8, 3, 8]
insertionSort(arr)
console.log(arr)

View File

@@ -1,8 +1,8 @@
/**
* Merge Sort is an algorithm where the main list is divided down into two half
* sized lists, which then have merge sort called on these two smaller lists
* sized lists, which then have merge sort called on these two smaller lists
* recursively until there is only a sorted list of one.
*
*
* On the way up the recursive calls, the lists will be merged together inserting
* the smaller value first, creating a larger sorted list.
*/
@@ -13,17 +13,17 @@
* @param {Array} list2 - sublist to break down
* @return {Array} merged list
*/
function merge(list1, list2) {
var results = [];
function merge (list1, list2) {
var results = []
while(list1.length && list2.length) {
while (list1.length && list2.length) {
if (list1[0] <= list2[0]) {
results.push(list1.shift());
results.push(list1.shift())
} else {
results.push(list2.shift());
results.push(list2.shift())
}
}
return results.concat(list1, list2);
return results.concat(list1, list2)
}
/**
@@ -31,19 +31,18 @@ function merge(list1, list2) {
* @param {Array} list - list to be sorted
* @return {Array} sorted list
*/
function mergeSort(list) {
if (list.length < 2) return list;
function mergeSort (list) {
if (list.length < 2) return list
var listHalf = Math.floor(list.length/2);
var subList1 = list.slice(0, listHalf);
var subList2 = list.slice(listHalf, list.length);
var listHalf = Math.floor(list.length / 2)
var subList1 = list.slice(0, listHalf)
var subList2 = list.slice(listHalf, list.length)
return merge(mergeSort(subList1), mergeSort(subList2));
return merge(mergeSort(subList1), mergeSort(subList2))
}
// Merge Sort Example
var unsortedArray = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1];
var sortedArray = mergeSort(unsortedArray);
console.log('Before:', unsortedArray, 'After:', sortedArray);
var unsortedArray = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1]
var sortedArray = mergeSort(unsortedArray)
console.log('Before:', unsortedArray, 'After:', sortedArray)

View File

@@ -2,37 +2,36 @@
* Quick sort is a comparison sorting algorithm that uses a divide and conquer strategy.
* For more information see here: https://en.wikipedia.org/wiki/Quicksort
*/
function quickSort(items) {
var length = items.length;
function quickSort (items) {
var length = items.length
if (length <= 1) {
return items;
return items
}
var PIVOT = items[0];
var GREATER = [];
var LESSER = [];
var PIVOT = items[0]
var GREATER = []
var LESSER = []
for (var i = 1; i < length; i++) {
if (items[i] > PIVOT) {
GREATER.push(items[i]);
GREATER.push(items[i])
} else {
LESSER.push(items[i]);
LESSER.push(items[i])
}
}
var sorted = quickSort(LESSER);
sorted.push(PIVOT);
sorted = sorted.concat(quickSort(GREATER));
return sorted;
var sorted = quickSort(LESSER)
sorted.push(PIVOT)
sorted = sorted.concat(quickSort(GREATER))
return sorted
}
//Implementation of quick sort
// Implementation of quick sort
var ar = [0, 5, 3, 2, 2];
//Array before Sort
console.log(ar);
ar = quickSort(ar);
//Array after sort
console.log(ar);
var ar = [0, 5, 3, 2, 2]
// Array before Sort
console.log(ar)
ar = quickSort(ar)
// Array after sort
console.log(ar)

View File

@@ -4,50 +4,49 @@
* significant position.
* For more information see: https://en.wikipedia.org/wiki/Radix_sort
*/
function radixSort(items, RADIX) {
//default radix is then because we usually count to base 10
function radixSort (items, RADIX) {
// default radix is then because we usually count to base 10
if (RADIX === undefined || RADIX < 1) {
RADIX = 10;
RADIX = 10
}
var maxLength = false;
var placement = 1;
var maxLength = false
var placement = 1
while (!maxLength) {
maxLength = true;
var buckets = [];
maxLength = true
var buckets = []
for (var i = 0; i < RADIX; i++) {
buckets.push([]);
buckets.push([])
}
for (var j = 0; j < items.length; j++) {
var tmp = items[j] / placement;
buckets[Math.floor(tmp % RADIX)].push(items[j]);
var tmp = items[j] / placement
buckets[Math.floor(tmp % RADIX)].push(items[j])
if (maxLength && tmp > 0) {
maxLength = false;
maxLength = false
}
}
var a = 0;
var a = 0
for (var b = 0; b < RADIX; b++) {
var buck = buckets[b];
var buck = buckets[b]
for (var k = 0; k < buck.length; k++) {
items[a] = buck[k];
a++;
items[a] = buck[k]
a++
}
}
placement *= RADIX;
placement *= RADIX
}
return items;
return items
}
//Implementation of radixSort
// Implementation of radixSort
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(ar);
radixSort(ar);
//Array after sort
console.log(ar);
var 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

@@ -1,4 +1,4 @@
/*The selection sort algorithm sorts an array by repeatedly finding the minimum element
/* The selection sort algorithm sorts an array by repeatedly finding the minimum element
*(considering ascending order) from unsorted part and putting it at the beginning. The
*algorithm maintains two subarrays in a given array.
*1) The subarray which is already sorted.
@@ -7,31 +7,31 @@
*In every iteration of selection sort, the minimum element (considering ascending order)
*from the unsorted subarray is picked and moved to the sorted subarray.
*/
function selectionSort(items) {
var length = items.length;
function selectionSort (items) {
var length = items.length
for (var i = 0; i < length - 1; i++) {
//Number of passes
var min = i; //min holds the current minimum number position for each pass; i holds the Initial min number
for (var j = i + 1; j < length; j++) { //Note that j = i + 1 as we only need to go through unsorted array
if (items[j] < items[min]) { //Compare the numbers
min = j; //Change the current min number position if a smaller num is found
// Number of passes
var min = i // min holds the current minimum number position for each pass; i holds the Initial min number
for (var j = i + 1; j < length; j++) { // Note that j = i + 1 as we only need to go through unsorted array
if (items[j] < items[min]) { // Compare the numbers
min = j // Change the current min number position if a smaller num is found
}
}
if (min != i) {
//After each pass, if the current min num != initial min num, exchange the position.
//Swap the numbers
var tmp = items[i];
items[i] = items[min];
items[min] = tmp;
// After each pass, if the current min num != initial min num, exchange the position.
// Swap the numbers
var tmp = items[i]
items[i] = items[min]
items[min] = tmp
}
}
}
//Implementation of Selection Sort
// Implementation of Selection Sort
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(ar);
selectionSort(ar);
//Array after sort
console.log(ar);
var ar = [5, 6, 7, 8, 1, 2, 12, 14]
// Array before Sort
console.log(ar)
selectionSort(ar)
// Array after sort
console.log(ar)

View File

@@ -3,38 +3,34 @@
* more information: https://en.wikipedia.org/wiki/Shellsort
*
*/
function shellSort(items) {
function shellSort (items) {
var interval = 1
var interval = 1;
while (interval < items.length / 3) {
interval = interval * 3 + 1
}
while (interval < items.length / 3) {
while (interval > 0) {
for (var outer = interval; outer < items.length; outer++) {
var value = items[outer]
var inner = outer
interval = interval * 3 + 1;
while (inner > interval - 1 && items[inner - interval] >= value) {
items[inner] = items[inner - interval]
inner = inner - interval
}
items[inner] = value
}
while (interval > 0) {
for (var outer = interval; outer < items.length; outer++) {
var value = items[outer];
var inner = outer;
while (inner > interval - 1 && items[inner - interval] >= value) {
items[inner] = items[inner - interval];
inner = inner - interval;
}
items[inner] = value;
}
interval = (interval - 1) / 3;
}
return items;
interval = (interval - 1) / 3
}
return items
}
//Implementation of shellSort
// Implementation of shellSort
var ar = [5, 6, 7, 8, 1, 2, 12, 14];
//Array before Sort
console.log(ar);
shellSort(ar);
//Array after sort
console.log(ar);
var 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

@@ -5,22 +5,22 @@
*/
Array.prototype.wiggleSort = function () {
for (let i = 0; i < this.length; ++i) {
const shouldNotBeLessThan = i % 2;
const isLessThan = this[i] < this[i + 1];
if (shouldNotBeLessThan && isLessThan) {
[this[i], this[i + 1]] = [this[i + 1], this[i]];
}
for (let i = 0; i < this.length; ++i) {
const shouldNotBeLessThan = i % 2
const isLessThan = this[i] < this[i + 1]
if (shouldNotBeLessThan && isLessThan) {
[this[i], this[i + 1]] = [this[i + 1], this[i]]
}
return this;
};
}
return this
}
//Implementation of wiggle sort
// Implementation of wiggle sort
var arr = [3, 5, 2, 1, 6, 4];
//Array before Wiggle Sort
console.log(arr); //[3, 5, 2, 1, 6, 4]
var 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 ]
// Array after wiggle sort
console.log(arr) // [ 3, 5, 2, 6, 1, 4 ]