Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added bogosort algorithem #147

Merged
merged 9 commits into from
Aug 9, 2023
16 changes: 16 additions & 0 deletions other/is_sorted_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* @function isSortedArray
* @description Checks if the array is sorted.
* @param {number[]} arr - array to check if it is sorted
* @returns {boolean} - true if the array is sorted and false if it's not sorted
* @example isSortedArray([1,2,3]) => true
* @example isSortedArray([9,2,3]) => false
*/
export function isSortedArray(arr: number[]): boolean {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] >= arr[i + 1]) {
return false;
}
}
return true;
}
8 changes: 8 additions & 0 deletions other/shuffle_array.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export function shuffleArray(arr: number[]) {
for (let i = arr.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
const temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
11 changes: 11 additions & 0 deletions other/test/is_sorted_array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { isSortedArray } from '../is_sorted_array';

describe('isSortedArray', () => {
test.each([
{ arr: [100], expected: true },
{ arr: [9, 2, 3], expected: false },
{ arr: [1, 2, 3], expected: true },
])('The return value of ($arr) should be $expected', ({ arr, expected }) => {
expect(isSortedArray(arr)).toEqual(expected);
});
});
25 changes: 25 additions & 0 deletions other/test/shuffle_array.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { shuffleArray } from '../shuffle_array';

describe('shuffleArray', () => {
test.each([{ arr: [1, 2, 3] }, { arr: [1, 2, 3, 6, 78, 2] }])(
"The length of the array $arr does'nt change after shuffling the array",
({ arr }) => {
const originalLength = arr.length;
shuffleArray(arr);
expect(arr.length).toEqual(originalLength);
}
);

test.each([{ arr: [1, 2, 3] }, { arr: [1, 2, 3, 6, 78, 2] }])(
'The elements of the array $arr remain the same (possibly with different order) after shuffling the array',
({ arr }) => {
const copyArray = Array.from(arr);
shuffleArray(arr);
expect(
arr.every((elem) => {
return copyArray.includes(elem);
})
).toEqual(true);
}
);
});
29 changes: 29 additions & 0 deletions sorts/bogo_sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { isSortedArray } from '../other/is_sorted_array';
import { shuffleArray } from '../other/shuffle_array';

/**
* @function bogoSort
* @description bogo sort is very simple to understand, it randomly shuffeles the input array until it is sorted
* @Complexity_Analysis
* Space complexity - O(1)
* Time complexity
*      Best case   -   O(n)
* The best case occurs when the array is already sorted.
*      Worst case  -   unbounded
* The worst case occurs when the shuffles never make the array sorted.
*      Average case -  O(n!n)
* The average case occurs when the shuffles sort the array after
* n! iterations (every iteration has a probability of 1/n! to sort the array),
* each iteration takes O(n) time.
*
* @param {number[]} arr - The input array
* @return {number[]} - The sorted array.
* @see [Bogo Sort](https://en.wikipedia.org/wiki/Bogosort)
* @example bogoSort([8, 3, 5, 1, 4, 2]) = [1, 2, 3, 4, 5, 8]
*/
export function bogoSort(arr: number[]): number[] {
while (!isSortedArray(arr)) {
shuffleArray(arr);
}
return arr;
}
15 changes: 15 additions & 0 deletions sorts/test/bogo_sort.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { bogoSort } from '../bogo_sort';

describe('BogoSort', () => {
test.each([
{ arr: [1], expectedResult: [1] },
{ arr: [2, 1], expectedResult: [1, 2] },
{ arr: [3, 1, 2], expectedResult: [1, 2, 3] },
{ arr: [3, 4, 1, 2], expectedResult: [1, 2, 3, 4] },
])(
'The return value of $arr should be $expectedResult',
({ arr, expectedResult }) => {
expect(bogoSort(arr)).toStrictEqual(expectedResult);
}
);
});