From 538a3d9e7274d9fc6ddb8314bd0931ae05d42c81 Mon Sep 17 00:00:00 2001 From: Solot Paul Date: Sun, 4 Oct 2020 15:46:01 +0300 Subject: [PATCH 1/8] Added TimSort and added JS Docs to some other functions --- Sorts/BogoSort.js | 41 +++++++++------ Sorts/BubbleSort.js | 78 ++++++++++++++++++----------- Sorts/IntroSort.js | 11 ++--- Sorts/TimSort.js | 118 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 197 insertions(+), 51 deletions(-) create mode 100644 Sorts/TimSort.js diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index 68d68edcc2..13d0ab312d 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -1,4 +1,5 @@ -/* +/** + * @function * A simple helper function that checks, if the array is * sorted in ascending order. */ @@ -22,7 +23,8 @@ Array.prototype.isSorted = function () { return true } -/* +/** + * @function * A simple helper function to shuffle the array randomly in place. */ Array.prototype.shuffle = function () { @@ -34,23 +36,32 @@ Array.prototype.shuffle = function () { } } -/* - * Implementation of the bogosort algorithm. This sorting algorithm randomly +/** + * @function 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 + * @param {Array} items array to be sorted + * @see [Bogosort](https://en.wikipedia.org/wiki/Bogosort) */ -function bogoSort (items) { +function bogoSort(items) { while (!items.isSorted()) { items.shuffle() } return items } - -// 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) +( +/** + * @function demo function to test bogosort. + */ +function demo() { + const size = 8 + let arr = Array(size) + for(let i = 0; i < size; i++) + { + arr[i] = Math.floor(Math.random() * 10); + } + // Array before BogoSort + console.log(arr) + bogoSort(arr) + // Array after BogoSort + console.log(arr) +})(); \ No newline at end of file diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index 3232991d1d..b8eccd090a 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -1,10 +1,14 @@ -/* Bubble Sort is a algorithm to sort an array. It -* compares adjacent element and swaps thier position -* The big O on bubble sort in worst and best case is O(N^2). +/** + * @function + * Bubble Sort is a algorithm to sort an array. It + * compares adjacent element and swaps thier position + * The big O on bubble sort in worst and best case is O(N^2). * Not efficient. + * @param {Array} items array to be sorted + * @see [BubbleSort](https://en.wikipedia.org/wiki/Bubble_sort) + * @returns {Array} sorted array */ - -function bubbleSort (items) { +function bubbleSort(items) { const length = items.length for (let i = (length - 1); i > 0; i--) { // Number of passes @@ -18,28 +22,35 @@ function bubbleSort (items) { } } -// Implementation of bubbleSort - -var ar = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log('-----before sorting-----') -console.log(ar) -bubbleSort(ar) -// Array after sort -console.log('-----after sorting-----') -console.log(ar) +( + /** + * @function Function to test Unoptimized BubbleSort + * Prints unsorted array and sorted array. + */ + function demo() { + let arr = [5, 6, 7, 8, 1, 2, 12, 14] + // Array before Sort + console.log('-----before sorting-----') + console.log(arr) + bubbleSort(arr) + // Array after sort + console.log('-----after sorting-----') + console.log(arr) + console.log(''); + } +)(); -/* alternative implementation of bubble sort algorithm. - Using a while loop instead. For educational purposses only - */ -/* -*In bubble sort, we keep iterating while something was swapped in -*the previous inner-loop iteration. By swapped I mean, in the -*inner loop iteration, we check each number if the number proceeding -*it is greater than itself, if so we swap them. +/** + * @function + * In bubble sort, we keep iterating while something was swapped in + * the previous inner-loop iteration. By swapped I mean, in the + * inner loop iteration, we check each number if the number proceeding + * it is greater than itself, if so we swap them. + * @param {Array} arr array to be sorted + * @returns {Array} sorted array */ -function alternativeBubbleSort (arr) { +function alternativeBubbleSort(arr) { let swapped = true while (swapped) { swapped = false @@ -53,9 +64,16 @@ function alternativeBubbleSort (arr) { return arr } -// test -console.log('-----before sorting-----') -var array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] -console.log(array) -console.log('-----after sorting-----') -console.log(alternativeBubbleSort(array)) + +( + /** + * @function Simple function to test optimized Bubble Sort. + */ + function demo() { + console.log('-----before sorting-----') + let array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] + console.log(array) + console.log('-----after sorting-----') + console.log(alternativeBubbleSort(array)) +} +)(); diff --git a/Sorts/IntroSort.js b/Sorts/IntroSort.js index e7ba322c5e..70c9026475 100644 --- a/Sorts/IntroSort.js +++ b/Sorts/IntroSort.js @@ -250,18 +250,17 @@ function introsort (array, compare) { (function demo () { const data = [] const size = 1000000 - var i = 0 - var temp - var c = function (a, b) { + let temp + let c = function (a, b) { return a - b } - for (i = 0; i < size; i++) { + for (let i = 0; i < size; i++) { temp = Math.random() * Number.MAX_SAFE_INTEGER data.push(temp) } introsort(data, c) - var faulty = false - for (i = 1; i < size; i++) { + let faulty = false + for (let i = 1; i < size; i++) { if (data[i] < data[i - 1]) { faulty = true break diff --git a/Sorts/TimSort.js b/Sorts/TimSort.js new file mode 100644 index 0000000000..1d6e7e64de --- /dev/null +++ b/Sorts/TimSort.js @@ -0,0 +1,118 @@ +/** + * @function Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, + * designed to perform well on many kinds of real-world data. + * It was implemented by Tim Peters in 2002 for use in the Python programming language. + * It is also used to sort arrays of non-primitive type in Java SE 7, + * on the Android platform, in GNU Octave, on V8, Swift and Rust. + * 1) It sorts small partitions using Insertion Sort. + * 2) Merges the partition using Merge Sort. + * @see [Timsort](https://en.wikipedia.org/wiki/Timsort) + * @param {Array} array + */ + +function Timsort(array) { + // Default size of a partition + const RUN = 32 + const n = array.length + // Sorting the partitions using Insertion Sort + for (let i = 0; i < n; i += RUN) { + InsertionSort(array, i, Math.min(i + RUN - 1, n - 1)) + } + for (let size = RUN; size < n; size *= 2) { + for (let left = 0; left < n; left += 2 * size) { + let mid = left + size - 1; + let right = Math.min(left + 2 * size - 1, n - 1); + Merge(array, left, mid, right) + } + } +} + +/** + * @function performs insertion sort on the partition + * @param {Array} array array to be sorted + * @param {Number} left left index of partiton + * @param {Number} right right index of partition + */ + +function InsertionSort(array, left, right) { + const n = array.length; + for (let i = left + 1; i <= right; i++) { + let key = array[i]; + let j = i - 1; + while (j >= left && array[j] > key) { + array[j + 1] = array[j]; + j--; + } + array[j + 1] = key; + } +} + +/** + * @function merges two sorted partitions + * @param {Array} array array to be sorted + * @param {Number} left left index of partition + * @param {Number} mid mid index of partition + * @param {Number} right right index of partition + */ + +function Merge(array, left, mid, right) { + if(mid >= right) return; + const n = array.length; + let len1 = mid - left + 1; + let len2 = right - mid; + let larr = Array(len1); + let rarr = Array(len2); + for(let i = 0; i < len1; i++) + { + larr[i] = array[left + i]; + } + for(let i = 0; i < len2; i++) + { + rarr[i] = array[mid + 1 + i]; + } + let i = 0, j = 0, k = left; + while (i < larr.length && j < rarr.length) { + if (larr[i] < rarr[j]) { + array[k++] = larr[i++]; + } + else { + array[k++] = rarr[j++]; + } + } + while (i < larr.length) { + array[k++] = larr[i++]; + } + while (j < rarr.length) { + array[k++] = rarr[j++]; + } +} + +(/** + * @example Test of Timsort functions. + * Data is randomly generated. + * Prints "RIGHT" if it works as expected, + * otherwise "FAULTY" + */ + function demo() { + let size = 1000000; + let data = Array(size) + for(let i = 0; i < size; i++) + { + data[i] = Math.random() * Number.MAX_SAFE_INTEGER; + } + let isSorted = function (array) { + const n = array.length + for (let i = 0; i < n - 1; i++) { + if (array[i] > array[i + 1]) return false; + } + return true; + } + Timsort(data); + if (isSorted(data)) { + console.log("RIGHT"); + } + else { + console.log("FAULTY"); + } + } +)(); \ No newline at end of file From b2f05fe784cbe8998f104c8579dabf3211af8c14 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 Date: Sun, 4 Oct 2020 17:31:10 +0300 Subject: [PATCH 2/8] Changed directory names --- {back-tracking => Backtracking}/KnightTour.js | 0 {back-tracking => Backtracking}/NQueen.js | 0 {back-tracking => Backtracking}/Sudoku.js | 0 {Project Euler => Project-Euler}/Problem1.js | 0 {TimingFunctions => Timing-Functions}/IntervalTimer.js | 0 5 files changed, 0 insertions(+), 0 deletions(-) rename {back-tracking => Backtracking}/KnightTour.js (100%) rename {back-tracking => Backtracking}/NQueen.js (100%) rename {back-tracking => Backtracking}/Sudoku.js (100%) rename {Project Euler => Project-Euler}/Problem1.js (100%) rename {TimingFunctions => Timing-Functions}/IntervalTimer.js (100%) diff --git a/back-tracking/KnightTour.js b/Backtracking/KnightTour.js similarity index 100% rename from back-tracking/KnightTour.js rename to Backtracking/KnightTour.js diff --git a/back-tracking/NQueen.js b/Backtracking/NQueen.js similarity index 100% rename from back-tracking/NQueen.js rename to Backtracking/NQueen.js diff --git a/back-tracking/Sudoku.js b/Backtracking/Sudoku.js similarity index 100% rename from back-tracking/Sudoku.js rename to Backtracking/Sudoku.js diff --git a/Project Euler/Problem1.js b/Project-Euler/Problem1.js similarity index 100% rename from Project Euler/Problem1.js rename to Project-Euler/Problem1.js diff --git a/TimingFunctions/IntervalTimer.js b/Timing-Functions/IntervalTimer.js similarity index 100% rename from TimingFunctions/IntervalTimer.js rename to Timing-Functions/IntervalTimer.js From 7419bda2fdb69d0a3d82f535953193c8ff6e18eb Mon Sep 17 00:00:00 2001 From: DarkWarrior703 Date: Sun, 4 Oct 2020 18:22:35 +0300 Subject: [PATCH 3/8] Added JS Docs to some sorting functions --- DIRECTORY.md | 1 + Sorts/BogoSort.js | 35 ++++---- Sorts/BubbleSort.js | 40 +++++----- Sorts/BucketSort.js | 20 ++--- Sorts/CocktailShakerSort.js | 29 ++++--- Sorts/CombSort.js | 49 +++++++----- Sorts/CountingSort.js | 28 ++++--- Sorts/CycleSort.js | 36 ++++++--- Sorts/FlashSort.js | 26 +++--- Sorts/GnomeSort.js | 9 ++- Sorts/IntroSort.js | 56 ++++++------- Sorts/TimSort.js | 155 +++++++++++++++++------------------- package.json | 4 +- 13 files changed, 257 insertions(+), 231 deletions(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index 7c35ecbfd3..b12fad70c0 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -122,6 +122,7 @@ * [RadixSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/RadixSort.js) * [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.js) * [ShellSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/ShellSort.js) + * [TimoSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TimoSort.js) * [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js) * [WiggleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/WiggleSort.js) diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index 13d0ab312d..b28986970c 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -1,8 +1,9 @@ -/** +/** * @function * A simple helper function that checks, if the array is * sorted in ascending order. - */ + * @returns {Boolean} true if the array is sorted, otherwise false + * */ // > [].isSorted() // true @@ -23,7 +24,7 @@ Array.prototype.isSorted = function () { return true } -/** +/** * @function * A simple helper function to shuffle the array randomly in place. */ @@ -41,8 +42,9 @@ Array.prototype.shuffle = function () { * rearranges the array until it is sorted. * @param {Array} items array to be sorted * @see [Bogosort](https://en.wikipedia.org/wiki/Bogosort) + * @returns {Array} sorted array */ -function bogoSort(items) { +function bogoSort (items) { while (!items.isSorted()) { items.shuffle() } @@ -52,16 +54,15 @@ function bogoSort(items) { /** * @function demo function to test bogosort. */ -function demo() { - const size = 8 - let arr = Array(size) - for(let i = 0; i < size; i++) - { - arr[i] = Math.floor(Math.random() * 10); - } - // Array before BogoSort - console.log(arr) - bogoSort(arr) - // Array after BogoSort - console.log(arr) -})(); \ No newline at end of file + function demo () { + const size = 5 + const arr = Array(size) + for (let i = 0; i < size; i++) { + arr[i] = Math.floor(Math.random() * 10) + } + // Array before BogoSort + console.log(arr) + bogoSort(arr) + // Array after BogoSort + console.log(arr) + })() diff --git a/Sorts/BubbleSort.js b/Sorts/BubbleSort.js index b8eccd090a..15dbd2a8d0 100644 --- a/Sorts/BubbleSort.js +++ b/Sorts/BubbleSort.js @@ -1,5 +1,5 @@ /** - * @function + * @function * Bubble Sort is a algorithm to sort an array. It * compares adjacent element and swaps thier position * The big O on bubble sort in worst and best case is O(N^2). @@ -8,7 +8,7 @@ * @see [BubbleSort](https://en.wikipedia.org/wiki/Bubble_sort) * @returns {Array} sorted array */ -function bubbleSort(items) { +function bubbleSort (items) { const length = items.length for (let i = (length - 1); i > 0; i--) { // Number of passes @@ -27,21 +27,18 @@ function bubbleSort(items) { * @function Function to test Unoptimized BubbleSort * Prints unsorted array and sorted array. */ - function demo() { - let arr = [5, 6, 7, 8, 1, 2, 12, 14] - // Array before Sort - console.log('-----before sorting-----') + function demo () { + const arr = [5, 6, 7, 8, 1, 2, 12, 14] + // Array before sort console.log(arr) bubbleSort(arr) // Array after sort - console.log('-----after sorting-----') console.log(arr) - console.log(''); } -)(); +)() /** - * @function + * @function * In bubble sort, we keep iterating while something was swapped in * the previous inner-loop iteration. By swapped I mean, in the * inner loop iteration, we check each number if the number proceeding @@ -50,7 +47,7 @@ function bubbleSort(items) { * @returns {Array} sorted array */ -function alternativeBubbleSort(arr) { +function alternativeBubbleSort (arr) { let swapped = true while (swapped) { swapped = false @@ -61,19 +58,20 @@ function alternativeBubbleSort(arr) { } } } - return arr } - ( /** * @function Simple function to test optimized Bubble Sort. */ - function demo() { - console.log('-----before sorting-----') - let array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] - console.log(array) - console.log('-----after sorting-----') - console.log(alternativeBubbleSort(array)) -} -)(); + function demo () { + const array = [10, 5, 3, 8, 2, 6, 4, 7, 9, 1] + // > alternativeBubbleSort(array) + // [1, 2, 3, 4, 5, 7, 8, 9, 10] + // Array before sort + console.log(array) + alternativeBubbleSort(array) + // Array after sort + console.log(array) + } +)() diff --git a/Sorts/BucketSort.js b/Sorts/BucketSort.js index 9423c4c104..c42f239be5 100644 --- a/Sorts/BucketSort.js +++ b/Sorts/BucketSort.js @@ -1,14 +1,14 @@ -/* -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 -distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. -Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons -and therefore can also be considered a comparison sort algorithm. The computational complexity estimates -involve the number of buckets. +/** + * 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 + * distribution sort, and is a cousin of radix sort in the most to least significant digit flavour. + * Bucket sort is a generalization of pigeonhole sort. Bucket sort can be implemented with comparisons + * and therefore can also be considered a comparison sort algorithm. The computational complexity estimates + * involve the number of buckets. -Time Complexity of Solution: -Best Case O(n); Average Case O(n); Worst Case O(n) + * Time Complexity of Solution: + * Best Case O(n); Average Case O(n); Worst Case O(n) */ function bucketSort (list, size) { diff --git a/Sorts/CocktailShakerSort.js b/Sorts/CocktailShakerSort.js index 337fa5f9f7..43594e4af9 100644 --- a/Sorts/CocktailShakerSort.js +++ b/Sorts/CocktailShakerSort.js @@ -1,8 +1,9 @@ -/* +/** + * @function * Cocktail shaker sort is a sort algorithm that is a bidirectional bubble sort - * more information: https://en.wikipedia.org/wiki/Cocktail_shaker_sort - * more information: https://en.wikipedia.org/wiki/Bubble_sort - * + * @see [Cocktail Shaker Sort]https://en.wikipedia.org/wiki/Cocktail_shaker_sort + * @see [Bubble Sort]https://en.wikipedia.org/wiki/Bubble_sort + * @param {Array} array to be sorted */ function cocktailShakerSort (items) { for (let i = items.length - 1; i > 0; i--) { @@ -30,11 +31,15 @@ function cocktailShakerSort (items) { } } -// 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) +( + /** + * @function A simple test function + */ + function demo () { + const ar = [5, 6, 7, 8, 1, 2, 12, 14] + // Array before Sort + console.log(ar) + cocktailShakerSort(ar) + // Array after sort + console.log(ar) + })() diff --git a/Sorts/CombSort.js b/Sorts/CombSort.js index ce34ce1577..8e3d679b85 100644 --- a/Sorts/CombSort.js +++ b/Sorts/CombSort.js @@ -1,18 +1,18 @@ -/* -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, -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 -a "shrink factor" k: [ n/k, n/k2, n/k3, ..., 1 ]. - +/** + * @function 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, + * 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 + * a "shrink factor" k: [ n/k, n/k2, n/k3, ..., 1 ]. + * @param {Array} array to be sorted + * @returns {Array} sorted array + * @see [CombSort]("https://en.wikipedia.org/wiki/Comb_sort") */ function combSort (list) { @@ -41,9 +41,16 @@ function combSort (list) { } return list } -const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(arrOrignal) -const arrSorted = combSort(arrOrignal) -// Array after sort -console.log(arrSorted) + +( + /** + * @function Test function + */ + function demo () { + const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14] + // Array before Sort + console.log(arrOrignal) + const arrSorted = combSort(arrOrignal) + // Array after sort + console.log(arrSorted) + })() diff --git a/Sorts/CountingSort.js b/Sorts/CountingSort.js index 56e2b24788..5a734f9b5c 100644 --- a/Sorts/CountingSort.js +++ b/Sorts/CountingSort.js @@ -1,8 +1,12 @@ -/* +/** + * @function * Counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; * that is, it is an integer sorting algorithm. - * more information: https://en.wikipedia.org/wiki/Counting_sort - * counting sort visualization: https://www.cs.usfca.edu/~galles/visualization/CountingSort.html + * @see [Counting Sort](https://en.wikipedia.org/wiki/Counting_sort) + * @see [Counting Sort Visualization](https://www.cs.usfca.edu/~galles/visualization/CountingSort.html) + * @param {Array} array array to be sorted + * @param {Number} min min element + * @param {Number} max max element */ function countingSort (arr, min, max) { @@ -27,11 +31,15 @@ function countingSort (arr, min, max) { return arr } -const arr = [3, 0, 2, 5, 4, 1] +( + /** + * @function Simple test function + */ + function demo () { + const arr = [3, 0, 2, 5, 4, 1] -// Array before Sort -console.log('-----before sorting-----') -console.log(arr) -// Array after sort -console.log('-----after sorting-----') -console.log(countingSort(arr, 0, 5)) + // Array before Sort + console.log(arr) + // Array after sort + console.log(countingSort(arr, 0, 5)) + })() diff --git a/Sorts/CycleSort.js b/Sorts/CycleSort.js index 5853712974..d973de29ab 100644 --- a/Sorts/CycleSort.js +++ b/Sorts/CycleSort.js @@ -1,9 +1,13 @@ -/* -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 + * 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. + * @param {Array} array array to be sorted + * @returns {Number} count of sort operations + * @see [Cycle sort](https://en.wikipedia.org/wiki/Cycle_sort) */ function cycleSort (list) { @@ -51,9 +55,17 @@ function cycleSort (list) { } return writes } -const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14] -// Array before Sort -console.log(arrOrignal) -cycleSort(arrOrignal) -// Array after sort -console.log(arrOrignal) + +( + /** + * @function Simple test function + */ + function demo () { + const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14] + // Array before Sort + console.log(arrOrignal) + cycleSort(arrOrignal) + // Array after sort + console.log(arrOrignal) + } +)() diff --git a/Sorts/FlashSort.js b/Sorts/FlashSort.js index 21dc000bed..5bfc1d3f76 100644 --- a/Sorts/FlashSort.js +++ b/Sorts/FlashSort.js @@ -1,7 +1,9 @@ -/* - * Flashsort is a distribution sorting algorithm showing linear computational complexity O(n) for uniformly distributed +/** + * @function Flashsort is a distribution sorting algorithm showing linear computational complexity O(n) for uniformly distributed * data sets and relatively little additional memory requirement. - * more information: https://en.wikipedia.org/wiki/Flashsort + * @see [FlashSort](https://en.wikipedia.org/wiki/Flashsort) + * @param {Array} array array to be sorted + * @returns {Array} sorted array */ function flashSort (arr) { @@ -43,7 +45,9 @@ function flashSort (arr) { arr[0] = hold // permutation - let move = 0; let t; let flash + let move = 0 + let t + let flash let j = 0 let k = m - 1 @@ -75,11 +79,11 @@ function flashSort (arr) { return arr } -const array = [3, 0, 2, 5, -1, 4, 1, -2] +(function demo () { + const array = [3, 0, 2, 5, -1, 4, 1, -2] -// Array before Sort -console.log('-----before sorting-----') -console.log(array) -// Array after sort -console.log('-----after sorting-----') -console.log(flashSort(array)) + // Array before Sort + console.log(array) + // Array after sort + console.log(flashSort(array)) +})() diff --git a/Sorts/GnomeSort.js b/Sorts/GnomeSort.js index d8ccd5ce02..95d96700e4 100644 --- a/Sorts/GnomeSort.js +++ b/Sorts/GnomeSort.js @@ -1,7 +1,8 @@ -/* +/** + * @function * Gnome sort is a sort algorithm that moving an element to its proper place is accomplished by a series of swap - * more information: https://en.wikipedia.org/wiki/Gnome_sort - * + * @see [GnomeSort](https://en.wikipedia.org/wiki/Gnome_sort) + * @param {Array} items array to be sorted */ function gnomeSort (items) { if (items.length <= 1) { @@ -23,7 +24,7 @@ function gnomeSort (items) { // Implementation of gnomeSort -var ar = [5, 6, 7, 8, 1, 2, 12, 14] +const ar = [5, 6, 7, 8, 1, 2, 12, 14] // Array before Sort console.log(ar) gnomeSort(ar) diff --git a/Sorts/IntroSort.js b/Sorts/IntroSort.js index 70c9026475..9a91bd4d10 100644 --- a/Sorts/IntroSort.js +++ b/Sorts/IntroSort.js @@ -28,12 +28,12 @@ function introsort (array, compare) { * 0 if a is equal to b * 1 if a greater than b */ - var defaultComparator = function (x, y) { + const defaultComparator = function (x, y) { if (x === undefined && y === undefined) return 0 if (x === undefined) return 1 if (y === undefined) return -1 - var xString = toString(x) - var yString = toString(y) + const xString = toString(x) + const yString = toString(y) if (xString < yString) return -1 if (xString > yString) return 1 return 0 @@ -45,7 +45,7 @@ function introsort (array, compare) { * @param {Object} obj * @returns {String} String representation of given object */ - var toString = function (obj) { + const toString = function (obj) { if (obj === null) return 'null' if (typeof obj === 'boolean' || typeof obj === 'number') { return obj.toString() @@ -75,8 +75,8 @@ function introsort (array, compare) { * [IIFE](https://en.wikipedia.org/wiki/Immediately_invoked_function_expression) */ return (function (array, comparator) { - var swap = function (index1, index2) { - var temp = array[index1] + const swap = function (index1, index2) { + const temp = array[index1] array[index1] = array[index2] array[index2] = temp } @@ -85,14 +85,14 @@ function introsort (array, compare) { * If the length of array is less than * this then we simply perform insertion sort */ - var THRESHOLD = 16 + const THRESHOLD = 16 /** * @constant TUNEMAXDEPTH * Constant usec to increase or decrease value * of maxDepth */ - var TUNEMAXDEPTH = 1 - var len = array.length + const TUNEMAXDEPTH = 1 + const len = array.length /** * Return if array is only of length 1 * Array of size 1 is always sorted @@ -104,7 +104,7 @@ function introsort (array, compare) { * Calculate maxDepth = log2(len) * Taken from implementation in stdc++ */ - var maxDepth = Math.floor(Math.log2(len)) * TUNEMAXDEPTH + const maxDepth = Math.floor(Math.log2(len)) * TUNEMAXDEPTH /** * The very first call to quicksort * this initiates sort routine @@ -133,7 +133,7 @@ function introsort (array, compare) { heapSort(start, last) return } - var pivot = (last + start) >> 1 + let pivot = (last + start) >> 1 pivot = partition(start, last, pivot) quickSort(start, pivot, depth - 1) quickSort(pivot + 1, last, depth - 1) @@ -148,8 +148,8 @@ function introsort (array, compare) { function partition (start, last, pivot) { swap(start, pivot) pivot = start - var lo = start - var hi = last + let lo = start + let hi = last while (true) { lo++ while (comparator(array[lo], array[pivot]) <= 0 && lo !== last) { @@ -175,9 +175,8 @@ function introsort (array, compare) { * @param {Number} last one more than last index of array to be sorted */ function insertionSort (start, last) { - var i, j - for (i = start + 1; i < last; i++) { - j = i - 1 + for (let i = start + 1; i < last; i++) { + let j = i - 1 while (j >= 0 && comparator(array[j], array[j + 1]) > 0) { swap(j, j + 1) j-- @@ -192,7 +191,7 @@ function introsort (array, compare) { * @param {Number} last one more than last index of array to be sorted */ function heapSort (start, last) { - var x = (last + start) >> 1 + let x = (last + start) >> 1 while (x - start >= 0) { heapify(x, start, last) x-- @@ -211,8 +210,8 @@ function introsort (array, compare) { * @param {Number} last one more than last index of segment that cur belongs to */ function heapify (cur, start, last) { - var size = last - start - var max, lt, rt + const size = last - start + let max, lt, rt cur = cur - start while (true) { max = cur @@ -249,13 +248,12 @@ function introsort (array, compare) { */ (function demo () { const data = [] - const size = 1000000 - let temp - let c = function (a, b) { + const size = 100000 + const c = function (a, b) { return a - b } for (let i = 0; i < size; i++) { - temp = Math.random() * Number.MAX_SAFE_INTEGER + const temp = Math.random() * Number.MAX_SAFE_INTEGER data.push(temp) } introsort(data, c) @@ -281,18 +279,16 @@ function introsort (array, compare) { (function demo () { const data = [] const data2 = [] - const size = 1000000 - var i = 0 - var temp - for (i = 0; i < size; i++) { - temp = Math.random() * Number.MAX_SAFE_INTEGER + const size = 100000 + for (let i = 0; i < size; i++) { + const temp = Math.random() * Number.MAX_SAFE_INTEGER data.push(temp) data2.push(temp) } introsort(data) data2.sort() - var faulty = false - for (i = 1; i < size; i++) { + let faulty = false + for (let i = 1; i < size; i++) { if (data[i] !== data2[i]) { faulty = true break diff --git a/Sorts/TimSort.js b/Sorts/TimSort.js index 1d6e7e64de..9139b6a9d1 100644 --- a/Sorts/TimSort.js +++ b/Sorts/TimSort.js @@ -1,30 +1,30 @@ -/** - * @function Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, - * designed to perform well on many kinds of real-world data. +/** + * @function Timsort is a hybrid stable sorting algorithm, derived from merge sort and insertion sort, + * designed to perform well on many kinds of real-world data. * It was implemented by Tim Peters in 2002 for use in the Python programming language. - * It is also used to sort arrays of non-primitive type in Java SE 7, + * It is also used to sort arrays of non-primitive type in Java SE 7, * on the Android platform, in GNU Octave, on V8, Swift and Rust. * 1) It sorts small partitions using Insertion Sort. * 2) Merges the partition using Merge Sort. - * @see [Timsort](https://en.wikipedia.org/wiki/Timsort) + * @see [Timsort](https://en.wikipedia.org/wiki/Timsort) * @param {Array} array */ -function Timsort(array) { - // Default size of a partition - const RUN = 32 - const n = array.length - // Sorting the partitions using Insertion Sort - for (let i = 0; i < n; i += RUN) { - InsertionSort(array, i, Math.min(i + RUN - 1, n - 1)) - } - for (let size = RUN; size < n; size *= 2) { - for (let left = 0; left < n; left += 2 * size) { - let mid = left + size - 1; - let right = Math.min(left + 2 * size - 1, n - 1); - Merge(array, left, mid, right) - } +function Timsort (array) { + // Default size of a partition + const RUN = 32 + const n = array.length + // Sorting the partitions using Insertion Sort + for (let i = 0; i < n; i += RUN) { + InsertionSort(array, i, Math.min(i + RUN - 1, n - 1)) + } + for (let size = RUN; size < n; size *= 2) { + for (let left = 0; left < n; left += 2 * size) { + const mid = left + size - 1 + const right = Math.min(left + 2 * size - 1, n - 1) + Merge(array, left, mid, right) } + } } /** @@ -34,17 +34,16 @@ function Timsort(array) { * @param {Number} right right index of partition */ -function InsertionSort(array, left, right) { - const n = array.length; - for (let i = left + 1; i <= right; i++) { - let key = array[i]; - let j = i - 1; - while (j >= left && array[j] > key) { - array[j + 1] = array[j]; - j--; - } - array[j + 1] = key; +function InsertionSort (array, left, right) { + for (let i = left + 1; i <= right; i++) { + const key = array[i] + let j = i - 1 + while (j >= left && array[j] > key) { + array[j + 1] = array[j] + j-- } + array[j + 1] = key + } } /** @@ -55,64 +54,58 @@ function InsertionSort(array, left, right) { * @param {Number} right right index of partition */ -function Merge(array, left, mid, right) { - if(mid >= right) return; - const n = array.length; - let len1 = mid - left + 1; - let len2 = right - mid; - let larr = Array(len1); - let rarr = Array(len2); - for(let i = 0; i < len1; i++) - { - larr[i] = array[left + i]; - } - for(let i = 0; i < len2; i++) - { - rarr[i] = array[mid + 1 + i]; - } - let i = 0, j = 0, k = left; - while (i < larr.length && j < rarr.length) { - if (larr[i] < rarr[j]) { - array[k++] = larr[i++]; - } - else { - array[k++] = rarr[j++]; - } - } - while (i < larr.length) { - array[k++] = larr[i++]; - } - while (j < rarr.length) { - array[k++] = rarr[j++]; +function Merge (array, left, mid, right) { + if (mid >= right) return + const len1 = mid - left + 1 + const len2 = right - mid + const larr = Array(len1) + const rarr = Array(len2) + for (let i = 0; i < len1; i++) { + larr[i] = array[left + i] + } + for (let i = 0; i < len2; i++) { + rarr[i] = array[mid + 1 + i] + } + let i = 0; let j = 0; let k = left + while (i < larr.length && j < rarr.length) { + if (larr[i] < rarr[j]) { + array[k++] = larr[i++] + } else { + array[k++] = rarr[j++] } + } + while (i < larr.length) { + array[k++] = larr[i++] + } + while (j < rarr.length) { + array[k++] = rarr[j++] + } } (/** * @example Test of Timsort functions. * Data is randomly generated. - * Prints "RIGHT" if it works as expected, + * Prints "RIGHT" if it works as expected, * otherwise "FAULTY" */ - function demo() { - let size = 1000000; - let data = Array(size) - for(let i = 0; i < size; i++) - { - data[i] = Math.random() * Number.MAX_SAFE_INTEGER; - } - let isSorted = function (array) { - const n = array.length - for (let i = 0; i < n - 1; i++) { - if (array[i] > array[i + 1]) return false; - } - return true; - } - Timsort(data); - if (isSorted(data)) { - console.log("RIGHT"); - } - else { - console.log("FAULTY"); - } + function demo () { + const size = 1000000 + const data = Array(size) + for (let i = 0; i < size; i++) { + data[i] = Math.random() * Number.MAX_SAFE_INTEGER + } + const isSorted = function (array) { + const n = array.length + for (let i = 0; i < n - 1; i++) { + if (array[i] > array[i + 1]) return false + } + return true + } + Timsort(data) + if (isSorted(data)) { + console.log('RIGHT') + } else { + console.log('FAULTY') } -)(); \ No newline at end of file + } +)() diff --git a/package.json b/package.json index afbded11b8..d9d4cf9c65 100644 --- a/package.json +++ b/package.json @@ -12,7 +12,7 @@ "node-fetch": "2.6.1" }, "devDependencies": { - "standard": "^14.3.4", - "doctest": "^0.17.1" + "doctest": "^0.17.1", + "standard": "^14.3.4" } } From 9cfbdcc3e58d13fcca6f1feb67af9ab56b248ae8 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 Date: Sun, 4 Oct 2020 18:34:45 +0300 Subject: [PATCH 4/8] Changed BogoSort --- Sorts/BogoSort.js | 9 --------- 1 file changed, 9 deletions(-) diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index b28986970c..1f5b438501 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -5,15 +5,6 @@ * @returns {Boolean} true if the array is sorted, otherwise false * */ -// > [].isSorted() -// true -// > [1].isSorted() -// true -// > [1,2,3].isSorted() -// true -// > [3,2,1].isSorted() -// false -/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */ Array.prototype.isSorted = function () { const length = this.length for (let i = 0; i < length - 1; i++) { From 1f8898e74af1919d3f56708c557bf5b13cfc6c6d Mon Sep 17 00:00:00 2001 From: DarkWarrior703 Date: Sun, 4 Oct 2020 18:43:57 +0300 Subject: [PATCH 5/8] Changed BogoSort --- Sorts/BogoSort.js | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index 1f5b438501..9a10ac6187 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -5,6 +5,16 @@ * @returns {Boolean} true if the array is sorted, otherwise false * */ + +// > [].isSorted() +// true +// > [1].isSorted() +// true +// > [1,2,3].isSorted() +// true +// > [3,2,1].isSorted() +// false +/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */ Array.prototype.isSorted = function () { const length = this.length for (let i = 0; i < length - 1; i++) { @@ -35,25 +45,17 @@ Array.prototype.shuffle = function () { * @see [Bogosort](https://en.wikipedia.org/wiki/Bogosort) * @returns {Array} sorted array */ -function bogoSort (items) { +function bogoSort(items) { while (!items.isSorted()) { items.shuffle() } return items } -( -/** - * @function demo function to test bogosort. - */ - function demo () { - const size = 5 - const arr = Array(size) - for (let i = 0; i < size; i++) { - arr[i] = Math.floor(Math.random() * 10) - } - // Array before BogoSort - console.log(arr) - bogoSort(arr) - // Array after BogoSort - console.log(arr) - })() +//Implementation of bogoSort +let arr = [3, 2, 6, 7] +// Array before BogoSort +console.log(arr) +bogoSort(arr) +// Array after BogoSort +console.log(arr) + From ae7a50ee601ce1360409a22653be5a9029d3a6ca Mon Sep 17 00:00:00 2001 From: DarkWarrior703 Date: Sun, 4 Oct 2020 18:45:44 +0300 Subject: [PATCH 6/8] Changed BogoSort --- Sorts/BogoSort.js | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/Sorts/BogoSort.js b/Sorts/BogoSort.js index 9a10ac6187..84aec6322f 100644 --- a/Sorts/BogoSort.js +++ b/Sorts/BogoSort.js @@ -5,7 +5,6 @@ * @returns {Boolean} true if the array is sorted, otherwise false * */ - // > [].isSorted() // true // > [1].isSorted() @@ -45,17 +44,16 @@ Array.prototype.shuffle = function () { * @see [Bogosort](https://en.wikipedia.org/wiki/Bogosort) * @returns {Array} sorted array */ -function bogoSort(items) { +function bogoSort (items) { while (!items.isSorted()) { items.shuffle() } return items } -//Implementation of bogoSort -let arr = [3, 2, 6, 7] +// Implementation of bogoSort +const arr = [3, 2, 6, 7] // Array before BogoSort console.log(arr) bogoSort(arr) // Array after BogoSort console.log(arr) - From f45d8dbd670c162e30cffb56e446ad661058a597 Mon Sep 17 00:00:00 2001 From: DarkWarrior703 <56077342+DarkWarrior703@users.noreply.github.com> Date: Sun, 4 Oct 2020 22:41:23 +0300 Subject: [PATCH 7/8] Changed DIRECTORY.md --- DIRECTORY.md | 1 - 1 file changed, 1 deletion(-) diff --git a/DIRECTORY.md b/DIRECTORY.md index b12fad70c0..7c35ecbfd3 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -122,7 +122,6 @@ * [RadixSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/RadixSort.js) * [SelectionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/SelectionSort.js) * [ShellSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/ShellSort.js) - * [TimoSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TimoSort.js) * [TopologicalSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/TopologicalSort.js) * [WiggleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/WiggleSort.js) From 1eebe2b650526572d132bb00fe9caf40fa409cc7 Mon Sep 17 00:00:00 2001 From: vinayak Date: Mon, 5 Oct 2020 22:12:13 +0530 Subject: [PATCH 8/8] Update TimSort.js --- Sorts/TimSort.js | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/Sorts/TimSort.js b/Sorts/TimSort.js index 9139b6a9d1..04c209f0f6 100644 --- a/Sorts/TimSort.js +++ b/Sorts/TimSort.js @@ -10,7 +10,7 @@ * @param {Array} array */ -function Timsort (array) { +const Timsort = (array) => { // Default size of a partition const RUN = 32 const n = array.length @@ -34,7 +34,7 @@ function Timsort (array) { * @param {Number} right right index of partition */ -function InsertionSort (array, left, right) { +const InsertionSort = (array, left, right) => { for (let i = left + 1; i <= right; i++) { const key = array[i] let j = i - 1 @@ -54,7 +54,7 @@ function InsertionSort (array, left, right) { * @param {Number} right right index of partition */ -function Merge (array, left, mid, right) { +const Merge = (array, left, mid, right) => { if (mid >= right) return const len1 = mid - left + 1 const len2 = right - mid @@ -82,30 +82,29 @@ function Merge (array, left, mid, right) { } } -(/** +/** * @example Test of Timsort functions. * Data is randomly generated. * Prints "RIGHT" if it works as expected, * otherwise "FAULTY" */ - function demo () { - const size = 1000000 - const data = Array(size) - for (let i = 0; i < size; i++) { - data[i] = Math.random() * Number.MAX_SAFE_INTEGER - } - const isSorted = function (array) { - const n = array.length - for (let i = 0; i < n - 1; i++) { - if (array[i] > array[i + 1]) return false - } - return true - } - Timsort(data) - if (isSorted(data)) { - console.log('RIGHT') - } else { - console.log('FAULTY') +(() => { + const size = 1000000 + const data = Array(size) + for (let i = 0; i < size; i++) { + data[i] = Math.random() * Number.MAX_SAFE_INTEGER + } + const isSorted = function (array) { + const n = array.length + for (let i = 0; i < n - 1; i++) { + if (array[i] > array[i + 1]) return false } + return true + } + Timsort(data) + if (isSorted(data)) { + console.log('RIGHT') + } else { + console.log('FAULTY') } -)() +})() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy