Skip to content

Commit b33f518

Browse files
authored
Fix bitonic and binary_insertion sorts (TheAlgorithms#563)
1 parent a34e66e commit b33f518

File tree

4 files changed

+41
-22
lines changed

4 files changed

+41
-22
lines changed

DIRECTORY.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
* [Fast Factorial](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/fast_factorial.rs)
1111
* [Hello Bigmath](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/hello_bigmath.rs)
1212
* [Poly1305](https://github.com/TheAlgorithms/Rust/blob/master/src/big_integer/poly1305.rs)
13+
* Bit Manipulation
14+
* [Counting Bits](https://github.com/TheAlgorithms/Rust/blob/master/src/bit_manipulation/counting_bits.rs)
1315
* Ciphers
1416
* [Aes](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/aes.rs)
1517
* [Another Rot13](https://github.com/TheAlgorithms/Rust/blob/master/src/ciphers/another_rot13.rs)
@@ -42,6 +44,7 @@
4244
* [B Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/b_tree.rs)
4345
* [Binary Search Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/binary_search_tree.rs)
4446
* [Fenwick Tree](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/fenwick_tree.rs)
47+
* [Floyds Algorithm](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/floyds_algorithm.rs)
4548
* [Graph](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/graph.rs)
4649
* [Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/heap.rs)
4750
* [Infix To Postfix](https://github.com/TheAlgorithms/Rust/blob/master/src/data_structures/infix_to_postfix.rs)
@@ -154,6 +157,7 @@
154157
* [Interpolation](https://github.com/TheAlgorithms/Rust/blob/master/src/math/interpolation.rs)
155158
* [Karatsuba Multiplication](https://github.com/TheAlgorithms/Rust/blob/master/src/math/karatsuba_multiplication.rs)
156159
* [Lcm Of N Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/lcm_of_n_numbers.rs)
160+
* [Leaky Relu](https://github.com/TheAlgorithms/Rust/blob/master/src/math/leaky_relu.rs)
157161
* [Linear Sieve](https://github.com/TheAlgorithms/Rust/blob/master/src/math/linear_sieve.rs)
158162
* [Matrix Ops](https://github.com/TheAlgorithms/Rust/blob/master/src/math/matrix_ops.rs)
159163
* [Mersenne Primes](https://github.com/TheAlgorithms/Rust/blob/master/src/math/mersenne_primes.rs)
@@ -174,6 +178,8 @@
174178
* [Signum](https://github.com/TheAlgorithms/Rust/blob/master/src/math/signum.rs)
175179
* [Simpson Integration](https://github.com/TheAlgorithms/Rust/blob/master/src/math/simpson_integration.rs)
176180
* [Sine](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sine.rs)
181+
* [Softmax](https://github.com/TheAlgorithms/Rust/blob/master/src/math/softmax.rs)
182+
* [Sprague Grundy Theorem](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sprague_grundy_theorem.rs)
177183
* [Square Pyramidal Numbers](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_pyramidal_numbers.rs)
178184
* [Square Root](https://github.com/TheAlgorithms/Rust/blob/master/src/math/square_root.rs)
179185
* [Sum Of Digits](https://github.com/TheAlgorithms/Rust/blob/master/src/math/sum_of_digits.rs)
@@ -197,6 +203,7 @@
197203
* [Kth Smallest Heap](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/kth_smallest_heap.rs)
198204
* [Linear Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/linear_search.rs)
199205
* [Quick Select](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/quick_select.rs)
206+
* [Saddleback Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/saddleback_search.rs)
200207
* [Ternary Search](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/ternary_search.rs)
201208
* [Ternary Search Min Max](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/ternary_search_min_max.rs)
202209
* [Ternary Search Min Max Recursive](https://github.com/TheAlgorithms/Rust/blob/master/src/searching/ternary_search_min_max_recursive.rs)

src/sorting/binary_insertion_sort.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
fn binary_search<T: Ord>(arr: &[T], target: &T) -> usize {
1+
fn _binary_search<T: Ord>(arr: &[T], target: &T) -> usize {
22
let mut low = 0;
33
let mut high = arr.len();
44

@@ -15,19 +15,18 @@ fn binary_search<T: Ord>(arr: &[T], target: &T) -> usize {
1515
low
1616
}
1717

18-
fn binary_insertion_sort<T: Ord + Clone>(arr: &mut [T]) {
18+
pub fn binary_insertion_sort<T: Ord + Clone>(arr: &mut [T]) {
1919
let len = arr.len();
2020

2121
for i in 1..len {
2222
let key = arr[i].clone();
23-
let index = binary_search(&arr[..i], &key);
24-
25-
arr[index..i+1].rotate_right(1);
23+
let index = _binary_search(&arr[..i], &key);
24+
25+
arr[index..i + 1].rotate_right(1);
2626
arr[index] = key;
2727
}
2828
}
2929

30-
3130
#[cfg(test)]
3231
mod tests {
3332
use super::*;

src/sorting/bitonic_sort.rs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,43 @@
1-
pub fn comp_and_swap<T: Ord>(array: &mut [T], index1: i32, index2: i32, direction: i32) {
2-
if (direction == 1 && array[index1 as usize] > array[index2 as usize])
3-
|| (direction == 0 && array[index1 as usize] < array[index2 as usize])
4-
{
5-
array.swap(index1 as usize, index2 as usize);
1+
fn _comp_and_swap<T: Ord>(array: &mut [T], left: usize, right: usize, ascending: bool) {
2+
if (ascending && array[left] > array[right]) || (!ascending && array[left] < array[right]) {
3+
array.swap(left, right);
64
}
75
}
86

9-
pub fn bitonic_merge<T: Ord>(array: &mut [T], low: i32, length: i32, direction: i32) {
7+
fn _bitonic_merge<T: Ord>(array: &mut [T], low: usize, length: usize, ascending: bool) {
108
if length > 1 {
119
let middle = length / 2;
1210
for i in low..(low + middle) {
13-
comp_and_swap(array, i, i + middle, direction);
11+
_comp_and_swap(array, i, i + middle, ascending);
1412
}
15-
bitonic_merge(array, low, middle, direction);
16-
bitonic_merge(array, low + middle, middle, direction);
13+
_bitonic_merge(array, low, middle, ascending);
14+
_bitonic_merge(array, low + middle, middle, ascending);
1715
}
1816
}
1917

20-
pub fn bitonic_sort<T: Ord>(array: &mut [T], low: i32, length: i32, direction: i32) {
18+
pub fn bitonic_sort<T: Ord>(array: &mut [T], low: usize, length: usize, ascending: bool) {
2119
if length > 1 {
2220
let middle = length / 2;
23-
bitonic_sort(array, low, middle, 1);
24-
bitonic_sort(array, low + middle, middle, 0);
25-
bitonic_merge(array, low, length, direction);
21+
bitonic_sort(array, low, middle, true);
22+
bitonic_sort(array, low + middle, middle, false);
23+
_bitonic_merge(array, low, length, ascending);
2624
}
2725
}
2826

2927
//Note that this program works only when size of input is a power of 2.
3028
#[cfg(test)]
3129
mod tests {
3230
use super::*;
31+
use crate::sorting::have_same_elements;
32+
use crate::sorting::is_descending_sorted;
33+
use crate::sorting::is_sorted;
3334

3435
#[test]
3536
fn descending() {
3637
//descending
3738
let mut ve1 = vec![6, 5, 4, 3];
3839
let cloned = ve1.clone();
39-
bitonic_sort(&mut ve1, 0, 4, 1);
40+
bitonic_sort(&mut ve1, 0, 4, true);
4041
assert!(is_sorted(&ve1) && have_same_elements(&ve1, &cloned));
4142
}
4243

@@ -45,7 +46,7 @@ mod tests {
4546
//pre-sorted
4647
let mut ve2 = vec![1, 2, 3, 4];
4748
let cloned = ve2.clone();
48-
bitonic_sort(&mut ve2, 0, 4, 0);
49-
assert!(is_sorted(&ve2) && have_same_elements(&ve2, &cloned));
49+
bitonic_sort(&mut ve2, 0, 4, false);
50+
assert!(is_descending_sorted(&ve2) && have_same_elements(&ve2, &cloned));
5051
}
5152
}

src/sorting/mod.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
mod bead_sort;
2+
mod binary_insertion_sort;
3+
mod bitonic_sort;
24
mod bogo_sort;
35
mod bubble_sort;
46
mod bucket_sort;
@@ -28,6 +30,8 @@ mod stooge_sort;
2830
mod tim_sort;
2931

3032
pub use self::bead_sort::bead_sort;
33+
pub use self::binary_insertion_sort::binary_insertion_sort;
34+
pub use self::bitonic_sort::bitonic_sort;
3135
pub use self::bogo_sort::bogo_sort;
3236
pub use self::bubble_sort::bubble_sort;
3337
pub use self::bucket_sort::bucket_sort;
@@ -90,6 +94,14 @@ where
9094
arr.windows(2).all(|w| w[0] <= w[1])
9195
}
9296

97+
#[cfg(test)]
98+
pub fn is_descending_sorted<T>(arr: &[T]) -> bool
99+
where
100+
T: cmp::PartialOrd,
101+
{
102+
arr.windows(2).all(|w| w[0] >= w[1])
103+
}
104+
93105
#[cfg(test)]
94106
mod tests {
95107
#[test]

0 commit comments

Comments
 (0)