forked from TheAlgorithms/JavaScript
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStoogeSort.js
More file actions
Latest commit
21 lines (21 loc) · 661 Bytes
/
Copy pathStoogeSort.js
File metadata and controls
21 lines (21 loc) · 661 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
* 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
*
*/
exportfunctionstoogeSort(items,leftEnd,rightEnd){
if(items[rightEnd-1]<items[leftEnd]){
consttemp=items[leftEnd]
items[leftEnd]=items[rightEnd-1]
items[rightEnd-1]=temp
}
constlength=rightEnd-leftEnd
if(length>2){
constthird=Math.floor(length/3)
stoogeSort(items,leftEnd,rightEnd-third)
stoogeSort(items,leftEnd+third,rightEnd)
stoogeSort(items,leftEnd,rightEnd-third)
}
returnitems
}