BogoSort.js: Simplify Array.isSorted() and add doctests

BogoSort.js: Simplify Array.isSorted() and add doctests
This commit is contained in:
John Law
2020-05-13 14:29:26 +02:00
committed by GitHub
4 changed files with 13 additions and 7 deletions

View File

@ -14,7 +14,8 @@ jobs:
node-version: ${{ matrix.node-version }}
- name: npm install, build, and test
run: |
npm install standard --save-dev
npm install doctest standard --save-dev
npx doctest Sorts/BogoSort.js Sorts/BucketSort.js
npx standard
cd Linear-Algebra-Javascript
npm ci

View File

@ -70,8 +70,8 @@
* [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CycleSort.js)
* [FlashSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/FlashSort.js)
* [GnomeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/GnomeSort.js)
* [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js)
* [Heapsort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/Heapsort.js)
* [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js)
* [InsertionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/InsertionSort.js)
* [MergeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/MergeSort.js)
* [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js)

View File

@ -3,14 +3,17 @@
* sorted in ascending order.
*/
// > [].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
if (length < 2) {
return true
}
for (let i = 0; i < length - 1; i++) {
if (this[i] > this[i + 1]) {
return false

View File

@ -55,6 +55,8 @@ function bucketSort (list, size) {
// Testing
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
// > bucketSort(arrOrignal)
// [1, 2, 5, 6, 7, 8, 12, 14]
// Array before Sort
console.log(arrOrignal)
const arrSorted = bucketSort(arrOrignal)