Skip to content

Commit 8fca284

Browse files
authored
BogoSort.js: Simplify Array.isSorted() and add doctests
BogoSort.js: Simplify Array.isSorted() and add doctests
2 parents 86b3622 + 3432ade commit 8fca284

File tree

4 files changed

+13
-7
lines changed

4 files changed

+13
-7
lines changed

.github/workflows/nodejs.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ jobs:
1414
node-version: ${{ matrix.node-version }}
1515
- name: npm install, build, and test
1616
run: |
17-
npm install standard --save-dev
17+
npm install doctest standard --save-dev
18+
npx doctest Sorts/BogoSort.js Sorts/BucketSort.js
1819
npx standard
1920
cd Linear-Algebra-Javascript
2021
npm ci

DIRECTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@
7070
* [CycleSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/CycleSort.js)
7171
* [FlashSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/FlashSort.js)
7272
* [GnomeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/GnomeSort.js)
73-
* [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js)
7473
* [Heapsort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/Heapsort.js)
74+
* [HeapSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/HeapSort.js)
7575
* [InsertionSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/InsertionSort.js)
7676
* [MergeSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/MergeSort.js)
7777
* [QuickSort](https://github.com/TheAlgorithms/Javascript/blob/master/Sorts/QuickSort.js)

Sorts/BogoSort.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
* sorted in ascending order.
44
*/
55

6+
// > [].isSorted()
7+
// true
8+
// > [1].isSorted()
9+
// true
10+
// > [1,2,3].isSorted()
11+
// true
12+
// > [3,2,1].isSorted()
13+
// false
614
/* eslint no-extend-native: ["off", { "exceptions": ["Object"] }] */
715
Array.prototype.isSorted = function () {
816
const length = this.length
9-
10-
if (length < 2) {
11-
return true
12-
}
13-
1417
for (let i = 0; i < length - 1; i++) {
1518
if (this[i] > this[i + 1]) {
1619
return false

Sorts/BucketSort.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ function bucketSort (list, size) {
5555

5656
// Testing
5757
const arrOrignal = [5, 6, 7, 8, 1, 2, 12, 14]
58+
// > bucketSort(arrOrignal)
59+
// [1, 2, 5, 6, 7, 8, 12, 14]
5860
// Array before Sort
5961
console.log(arrOrignal)
6062
const arrSorted = bucketSort(arrOrignal)

0 commit comments

Comments
 (0)