mirror of
https://github.com/TheAlgorithms/JavaScript.git
synced 2025-07-07 11:08:54 +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 }}
|
||||
- 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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
Reference in New Issue
Block a user