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
38 changes: 38 additions & 0 deletions sorts/bogo_sort.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @function bubbleSort
* @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(1)
* The best case occurs when the first shuufle sorts the array.
*      Worst case  -   unbounded
* The worst case occurs when the suffeles never make the array sorted.
*      Average case -  unbounded
mendlero marked this conversation as resolved.
Show resolved Hide resolved
* The average case occurs when the suffeles never make the array sorted.
*
* @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)) {
for (var i = arr.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
return arr;
}

function isSortedArray(arr: number[]): boolean {
for (let i = 0; i < arr.length - 1; i++) {
if (arr[i] >= arr[i + 1]) {
return false;
}
}
return true;
}