Skip to content

Commit a1761a4

Browse files
authored
Add stooge sort (rust-lang#224)
1 parent b605d68 commit a1761a4

File tree

5 files changed

+78
-0
lines changed

5 files changed

+78
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
* [Radix Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/radix_sort.rs)
5151
* [Selection Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/selection_sort.rs)
5252
* [Shell Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/shell_sort.rs)
53+
* [Stooge Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/stooge_sort.rs)
5354
* String
5455
* [Knuth Morris Pratt](https://github.com/TheAlgorithms/Rust/blob/master/src/string/knuth_morris_pratt.rs)
5556
* [Manacher](https://github.com/TheAlgorithms/Rust/blob/master/src/string/manacher.rs)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ RESTART BUILD
1717
- [x] [Radix](./src/sorting/radix_sort.rs)
1818
- [x] [Selection](./src/sorting/selection_sort.rs)
1919
- [x] [Shell](./src/sorting/shell_sort.rs)
20+
- [x] [Stooge](./src/sorting/stooge_sort.rs)
2021

2122
## Graphs
2223

src/sorting/README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,14 @@ __Properties__
100100

101101
###### View the algorithm in [action][shell-toptal]
102102

103+
### [Stooge](./stooge_sort.rs)
104+
![alt text][stooge-image]
105+
106+
From [Wikipedia][stooge-wiki]: Stooge sort is a recursive sorting algorithm. It is notable for its exceptionally bad time complexity of O(n^(log 3 / log 1.5)) = O(n^2.7095...). The running time of the algorithm is thus slower compared to reasonable sorting algorithms, and is slower than Bubble sort, a canonical example of a fairly inefficient sort. It is however more efficient than Slowsort. The name comes from The Three Stooges.
107+
108+
__Properties__
109+
* Worst case performance O(n^(log(3) / log(1.5)))
110+
103111
[bubble-toptal]: https://www.toptal.com/developers/sorting-algorithms/bubble-sort
104112
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
105113
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
@@ -128,3 +136,6 @@ __Properties__
128136
[shell-toptal]: https://www.toptal.com/developers/sorting-algorithms/shell-sort
129137
[shell-wiki]: https://en.wikipedia.org/wiki/Shellsort
130138
[shell-image]: https://upload.wikimedia.org/wikipedia/commons/d/d8/Sorting_shellsort_anim.gif "Shell Sort"
139+
140+
[stooge-image]: https://upload.wikimedia.org/wikipedia/commons/f/f8/Sorting_stoogesort_anim.gif
141+
[stooge-wiki]: https://en.wikipedia.org/wiki/Stooge_sort

src/sorting/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ mod quick_sort;
77
mod radix_sort;
88
mod selection_sort;
99
mod shell_sort;
10+
mod stooge_sort;
1011

1112
pub use self::bubble_sort::bubble_sort;
1213
pub use self::counting_sort::counting_sort;
@@ -18,6 +19,7 @@ pub use self::quick_sort::quick_sort;
1819
pub use self::radix_sort::radix_sort;
1920
pub use self::selection_sort::selection_sort;
2021
pub use self::shell_sort::shell_sort;
22+
pub use self::stooge_sort::stooge_sort;
2123

2224
use std::cmp;
2325

src/sorting/stooge_sort.rs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
fn _stooge_sort<T: Ord>(arr: &mut [T], start: usize, end: usize) {
2+
if arr[start] > arr[end] {
3+
arr.swap(start, end);
4+
}
5+
6+
if start + 1 >= end {
7+
return;
8+
}
9+
10+
let k = (end - start + 1) / 3;
11+
12+
_stooge_sort(arr, start, end - k);
13+
_stooge_sort(arr, start + k, end);
14+
_stooge_sort(arr, start, end - k);
15+
}
16+
17+
pub fn stooge_sort<T: Ord>(arr: &mut [T]) {
18+
let len = arr.len();
19+
if len == 0 {
20+
return;
21+
}
22+
23+
_stooge_sort(arr, 0, len - 1);
24+
}
25+
26+
#[cfg(test)]
27+
mod test {
28+
use super::*;
29+
30+
#[test]
31+
fn basic() {
32+
let mut vec = vec![3, 5, 6, 3, 1, 4];
33+
stooge_sort(&mut vec);
34+
for i in 0..vec.len() - 1 {
35+
assert!(vec[i] <= vec[i + 1]);
36+
}
37+
}
38+
39+
#[test]
40+
fn empty() {
41+
let mut vec: Vec<i32> = vec![];
42+
stooge_sort(&mut vec);
43+
assert_eq!(vec, vec![]);
44+
}
45+
46+
#[test]
47+
fn reverse() {
48+
let mut vec = vec![6, 5, 4, 3, 2, 1];
49+
stooge_sort(&mut vec);
50+
for i in 0..vec.len() - 1 {
51+
assert!(vec[i] <= vec[i + 1]);
52+
}
53+
}
54+
55+
#[test]
56+
fn already_sorted() {
57+
let mut vec = vec![1, 2, 3, 4, 5, 6];
58+
stooge_sort(&mut vec);
59+
for i in 0..vec.len() - 1 {
60+
assert!(vec[i] <= vec[i + 1]);
61+
}
62+
}
63+
}

0 commit comments

Comments
 (0)