Skip to content

Commit a4c8d37

Browse files
authored
added bogosort algorithem (TheAlgorithms#147)
* added bogo sort algorithem * Revert "added bogo sort algorithem" This reverts commit 5a512df. * feat: added bogosort algorithem * refactor: split bogosort into 2 seperate algorithems * test: added tests to shuffle_array and is_sorted_array * test: added test for bogo sort algorithem * refactor: changed the complexity analysis of the bogosort algorithem * test: updated the test to use test.each * refactor: removed the description of shuffleArray
1 parent 7988713 commit a4c8d37

File tree

6 files changed

+104
-0
lines changed

6 files changed

+104
-0
lines changed

other/is_sorted_array.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* @function isSortedArray
3+
* @description Checks if the array is sorted.
4+
* @param {number[]} arr - array to check if it is sorted
5+
* @returns {boolean} - true if the array is sorted and false if it's not sorted
6+
* @example isSortedArray([1,2,3]) => true
7+
* @example isSortedArray([9,2,3]) => false
8+
*/
9+
export function isSortedArray(arr: number[]): boolean {
10+
for (let i = 0; i < arr.length - 1; i++) {
11+
if (arr[i] >= arr[i + 1]) {
12+
return false;
13+
}
14+
}
15+
return true;
16+
}

other/shuffle_array.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function shuffleArray(arr: number[]) {
2+
for (let i = arr.length - 1; i > 0; i--) {
3+
const j = Math.floor(Math.random() * (i + 1));
4+
const temp = arr[i];
5+
arr[i] = arr[j];
6+
arr[j] = temp;
7+
}
8+
}

other/test/is_sorted_array.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { isSortedArray } from '../is_sorted_array';
2+
3+
describe('isSortedArray', () => {
4+
test.each([
5+
{ arr: [100], expected: true },
6+
{ arr: [9, 2, 3], expected: false },
7+
{ arr: [1, 2, 3], expected: true },
8+
])('The return value of ($arr) should be $expected', ({ arr, expected }) => {
9+
expect(isSortedArray(arr)).toEqual(expected);
10+
});
11+
});

other/test/shuffle_array.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { shuffleArray } from '../shuffle_array';
2+
3+
describe('shuffleArray', () => {
4+
test.each([{ arr: [1, 2, 3] }, { arr: [1, 2, 3, 6, 78, 2] }])(
5+
"The length of the array $arr does'nt change after shuffling the array",
6+
({ arr }) => {
7+
const originalLength = arr.length;
8+
shuffleArray(arr);
9+
expect(arr.length).toEqual(originalLength);
10+
}
11+
);
12+
13+
test.each([{ arr: [1, 2, 3] }, { arr: [1, 2, 3, 6, 78, 2] }])(
14+
'The elements of the array $arr remain the same (possibly with different order) after shuffling the array',
15+
({ arr }) => {
16+
const copyArray = Array.from(arr);
17+
shuffleArray(arr);
18+
expect(
19+
arr.every((elem) => {
20+
return copyArray.includes(elem);
21+
})
22+
).toEqual(true);
23+
}
24+
);
25+
});

sorts/bogo_sort.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { isSortedArray } from '../other/is_sorted_array';
2+
import { shuffleArray } from '../other/shuffle_array';
3+
4+
/**
5+
* @function bogoSort
6+
* @description bogo sort is very simple to understand, it randomly shuffeles the input array until it is sorted
7+
* @Complexity_Analysis
8+
* Space complexity - O(1)
9+
* Time complexity
10+
*      Best case   -   O(n)
11+
* The best case occurs when the array is already sorted.
12+
*      Worst case  -   unbounded
13+
* The worst case occurs when the shuffles never make the array sorted.
14+
*      Average case -  O(n!n)
15+
* The average case occurs when the shuffles sort the array after
16+
* n! iterations (every iteration has a probability of 1/n! to sort the array),
17+
* each iteration takes O(n) time.
18+
*
19+
* @param {number[]} arr - The input array
20+
* @return {number[]} - The sorted array.
21+
* @see [Bogo Sort](https://en.wikipedia.org/wiki/Bogosort)
22+
* @example bogoSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8]
23+
*/
24+
export function bogoSort(arr: number[]): number[] {
25+
while (!isSortedArray(arr)) {
26+
shuffleArray(arr);
27+
}
28+
return arr;
29+
}

sorts/test/bogo_sort.test.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { bogoSort } from '../bogo_sort';
2+
3+
describe('BogoSort', () => {
4+
test.each([
5+
{ arr: [1], expectedResult: [1] },
6+
{ arr: [2, 1], expectedResult: [1, 2] },
7+
{ arr: [3, 1, 2], expectedResult: [1, 2, 3] },
8+
{ arr: [3, 4, 1, 2], expectedResult: [1, 2, 3, 4] },
9+
])(
10+
'The return value of $arr should be $expectedResult',
11+
({ arr, expectedResult }) => {
12+
expect(bogoSort(arr)).toStrictEqual(expectedResult);
13+
}
14+
);
15+
});

0 commit comments

Comments
 (0)