mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-09 13:46:22 +08:00
BogoSort.js: Simplify Array.isSorted() and add doctests
BogoSort.js: Simplify Array.isSorted() and add doctests
This commit is contained in:
3
.github/workflows/nodejs.yml
vendored
3
.github/workflows/nodejs.yml
vendored
@ -14,7 +14,8 @@ jobs:
|
|||||||
node-version: ${{ matrix.node-version }}
|
node-version: ${{ matrix.node-version }}
|
||||||
- name: npm install, build, and test
|
- name: npm install, build, and test
|
||||||
run: |
|
run: |
|
||||||
npm install standard --save-dev
|
npm install doctest standard --save-dev
|
||||||
|
npx doctest Sorts/BogoSort.js Sorts/BucketSort.js
|
||||||
npx standard
|
npx standard
|
||||||
cd Linear-Algebra-Javascript
|
cd Linear-Algebra-Javascript
|
||||||
npm ci
|
npm ci
|
||||||
|
@ -70,8 +70,8 @@
|
|||||||
* [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CycleSort.js)
|
* [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CycleSort.js)
|
||||||
* [FlashSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/FlashSort.js)
|
* [FlashSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/FlashSort.js)
|
||||||
* [GnomeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/GnomeSort.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)
|
||||||
|
* [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js)
|
||||||
* [InsertionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/InsertionSort.js)
|
* [InsertionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/InsertionSort.js)
|
||||||
* [MergeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/MergeSort.js)
|
* [MergeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/MergeSort.js)
|
||||||
* [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js)
|
* [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js)
|
||||||
|
@ -3,14 +3,17 @@
|
|||||||
* sorted in ascending order.
|
* 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"] }] */
|
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
|
||||||
Array.prototype.isSorted = function () {
|
Array.prototype.isSorted = function () {
|
||||||
const length = this.length
|
const length = this.length
|
||||||
|
|
||||||
if (length < 2) {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = 0; i < length - 1; i++) {
|
for (let i = 0; i < length - 1; i++) {
|
||||||
if (this[i] > this[i + 1]) {
|
if (this[i] > this[i + 1]) {
|
||||||
return false
|
return false
|
||||||
|
@ -55,6 +55,8 @@ function bucketSort (list, size) {
|
|||||||
|
|
||||||
// Testing
|
// Testing
|
||||||
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
|
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
|
||||||
|
// > bucketSort(arrOrignal)
|
||||||
|
// [1, 2, 5, 6, 7, 8, 12, 14]
|
||||||
// Array before Sort
|
// Array before Sort
|
||||||
console.log(arrOrignal)
|
console.log(arrOrignal)
|
||||||
const arrSorted = bucketSort(arrOrignal)
|
const arrSorted = bucketSort(arrOrignal)
|
||||||
|
Reference in New Issue
Block a user