Files
JavaScript/Sorts/StoogeSort.js
Madiena 2e18fbb39a merge: Add the Stooge Sort Algorithm (#998)
* Add stooge sort sorting algorithm with included tests

* Add stooge sort sorting algorithm with included tests

* Add correct url for more information

* Add time complexity warning
2022-04-28 13:47:23 +05:30

22 lines
662 B
JavaScript

/*
* Stooge Sort sorts an array based on divide and conquer principle
* note the exceptionally bad time complexity
* more information: https://en.wikipedia.org/wiki/Stooge_sort
*
*/
export function stoogeSort (items, leftEnd, rightEnd) {
if (items[rightEnd - 1] < items[leftEnd]) {
const temp = items[leftEnd]
items[leftEnd] = items[rightEnd - 1]
items[rightEnd - 1] = temp
}
const length = rightEnd - leftEnd
if (length > 2) {
const third = Math.floor(length / 3)
stoogeSort(items, leftEnd, rightEnd - third)
stoogeSort(items, leftEnd + third, rightEnd)
stoogeSort(items, leftEnd, rightEnd - third)
}
return items
}