Skip to content

Commit 1aea1bb

Browse files
authored
Add cocktail shaker sort (TheAlgorithms#221)
1 parent a1761a4 commit 1aea1bb

5 files changed

Lines changed: 87 additions & 0 deletions

File tree

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
* [Linear Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/linear_search.rs)
4343
* Sorting
4444
* [Bubble Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/bubble_sort.rs)
45+
* [Cocktail-Shaker Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/cocktail_shaker_sort.rs)
4546
* [Counting Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/counting_sort.rs)
4647
* [Heap Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/heap_sort.rs)
4748
* [Insertion Sort](https://github.com/TheAlgorithms/Rust/blob/master/src/sorting/insertion_sort.rs)

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ RESTART BUILD
99
## [Sort Algorithms](./src/sorting)
1010

1111
- [x] [Bubble](./src/sorting/bubble_sort.rs)
12+
- [x] [Cocktail-Shaker](./src/sorting/cocktail_shaker_sort.rs)
1213
- [x] [Counting](./src/sorting/counting_sort.rs)
1314
- [x] [Heap](./src/sorting/heap_sort.rs)
1415
- [x] [Insertion](./src/sorting/insertion_sort.rs)

src/sorting/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,18 @@ __Properties__
1515

1616

1717

18+
### [Cocktail-Shaker](./cocktail_shaker_sort.rs)
19+
![alt text][shaker-image]
20+
21+
From [Wikipedia][shaker-wiki]: Cocktail shaker sort, also known as bidirectional bubble sort, cocktail sort, shaker sort (which can also refer to a variant of selection sort), ripple sort, shuffle sort, or shuttle sort, is an extension of bubble sort. The algorithm extends bubble sort by operating in two directions. While it improves on bubble sort by more quickly moving items to the beginning of the list, it provides only marginal performance improvements.
22+
23+
__Properties__
24+
* Worst case performance O(n^2)
25+
* Best case performance O(n)
26+
* Average case performance O(n^2)
27+
28+
29+
1830
### [Counting](./counting_sort.rs)
1931

2032
From [Wikipedia][counting-wiki]: In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.
@@ -112,6 +124,9 @@ __Properties__
112124
[bubble-wiki]: https://en.wikipedia.org/wiki/Bubble_sort
113125
[bubble-image]: https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/Bubblesort-edited-color.svg/220px-Bubblesort-edited-color.svg.png "Bubble Sort"
114126

127+
[shaker-wiki]: https://en.wikipedia.org/wiki/Cocktail_shaker_sort
128+
[shaker-image]: https://upload.wikimedia.org/wikipedia/commons/e/ef/Sorting_shaker_sort_anim.gif
129+
115130
[counting-wiki]: https://en.wikipedia.org/wiki/Counting_sort
116131

117132
[insertion-toptal]: https://www.toptal.com/developers/sorting-algorithms/insertion-sort
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
pub fn cocktail_shaker_sort<T: Ord>(arr: &mut [T]) {
2+
let len = arr.len();
3+
4+
if len == 0 {
5+
return;
6+
}
7+
8+
loop {
9+
let mut swapped = false;
10+
11+
for i in 0..(len - 1).clamp(0, len) {
12+
if arr[i] > arr[i + 1] {
13+
arr.swap(i, i + 1);
14+
swapped = true;
15+
}
16+
}
17+
18+
if !swapped {
19+
break;
20+
}
21+
22+
swapped = false;
23+
24+
for i in (0..(len - 1).clamp(0, len)).rev() {
25+
if arr[i] > arr[i + 1] {
26+
arr.swap(i, i + 1);
27+
swapped = true;
28+
}
29+
}
30+
31+
if !swapped {
32+
break;
33+
}
34+
}
35+
}
36+
37+
#[cfg(test)]
38+
mod tests {
39+
use super::*;
40+
41+
#[test]
42+
fn basic() {
43+
let mut arr = vec![5, 2, 1, 3, 4, 6];
44+
cocktail_shaker_sort(&mut arr);
45+
assert_eq!(arr, vec![1, 2, 3, 4, 5, 6]);
46+
}
47+
48+
#[test]
49+
fn empty() {
50+
let mut arr = Vec::<i32>::new();
51+
cocktail_shaker_sort(&mut arr);
52+
assert_eq!(arr, vec![]);
53+
}
54+
55+
#[test]
56+
fn one_element() {
57+
let mut arr = vec![1];
58+
cocktail_shaker_sort(&mut arr);
59+
assert_eq!(arr, vec![1]);
60+
}
61+
62+
#[test]
63+
fn pre_sorted() {
64+
let mut arr = vec![1, 2, 3, 4, 5, 6];
65+
cocktail_shaker_sort(&mut arr);
66+
assert_eq!(arr, vec![1, 2, 3, 4, 5, 6]);
67+
}
68+
}

src/sorting/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
mod bubble_sort;
2+
mod cocktail_shaker_sort;
23
mod counting_sort;
34
mod heap_sort;
45
mod insertion_sort;
@@ -10,6 +11,7 @@ mod shell_sort;
1011
mod stooge_sort;
1112

1213
pub use self::bubble_sort::bubble_sort;
14+
pub use self::cocktail_shaker_sort::cocktail_shaker_sort;
1315
pub use self::counting_sort::counting_sort;
1416
pub use self::counting_sort::generic_counting_sort;
1517
pub use self::heap_sort::heap_sort;

0 commit comments

Comments
 (0)