8000 Merge pull request #1130 from i-vishi/dev · glitches-coder/Algorithms@861e65f · GitHub
[go: up one dir, main page]

Skip to content

Commit 861e65f

Browse files
authored
Merge pull request VAR-solutions#1130 from i-vishi/dev
added quick sort, merge, counting sort in Rust
2 parents 2602759 + cbf9a7f commit 861e65f

File tree

5 files changed

+162
-43
lines changed

5 files changed

+162
-43
lines changed

Sorting/BubbleSortPHP/bubblesort.php renamed to Sorting/Bubble Sort/PHP/bubble-sort.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ function print_array($array) {
2525
print_array($array);
2626
bubble_sort($array);
2727

28-
?>
28+
?>
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/// Counting sort
2+
3+
/// * `arr` - Collection of value to be sorted in place.
4+
5+
/// * `min` - Lower bound of the integer range.
6+
7+
/// * `max` - Upper bound of the integer range.
8+
9+
/// * `key` - Function extracting key witn the integer range from elements.
10+
11+
pub fn counting_sort<F, T>(arr: &mut [T], min: usize, max: usize, key: F)
12+
where
13+
F: Fn(&T) -> usize,
14+
T: Clone,
15+
{
16+
let mut prefix_sums = {
17+
// 1. Initialize the count array with default value 0.
18+
let len = max - min;
19+
let mut count_arr = Vec::with_capacity(len);
20+
count_arr.resize(len, 0);
21+
22+
// 2. Scan elements to collect counts.
23+
for value in arr.iter() {
24+
count_arr[key(value)] += 1;
25+
}
26+
27+
// 3. Calculate prefix sum.
28+
count_arr
29+
.into_iter()
30+
.scan(0, |state, x| {
31+
*state += x;
32+
Some(*state - x)
33+
})
34+
.collect::<Vec<usize>>()
35+
};
36+
37+
// 4. Use prefix sum as index position of output element.
38+
for value in arr.to_vec().iter() {
39+
let index = key(value);
40+
arr[prefix_sums[index]] = value.clone();
41+
prefix_sums[index] += 1;
42+
}
43+
}
44+
45+
#[cfg(test)]
46+
47+
mod base {
48+
use super::*;
49+
fn counting_sort_(arr: &mut [i32]) {
50+
counting_sort(arr, 1, 10, |int| *int as usize);
51+
}
52+
base_cases!(counting_sort_);
53+
}
54+
55+
#[cfg(test)]
56+
57+
mod stability {
58+
use super::*;
59+
fn counting_sort_(arr: &mut [(i32, i32)]) {
60+
counting_sort(arr, 1, 10, |t| t.0 as usize);
61+
}
62+
stability_cases!(counting_sort_);
63+
}

Sorting/Merge Sort/Rust/merge-sort.rs

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
fn merge<T: Copy + PartialOrd>(x1: &[T], x2: &[T], y: &mut [T]) {
2+
assert_eq!(x1.len() + x2.len(), y.len());
3+
let mut i = 0;
4+
let mut j = 0;
5+
let mut k = 0;
6+
while i < x1.len() && j < x2.len() {
7+
if x1[i] < x2[j] {
8+
y[k] = x1[i];
9+
k += 1;
10+
i += 1;
11+
} else {
12+
y[k] = x2[j];
13+
k += 1;
14+
j += 1;
15+
}
16+
}
17 A93C +
if i < x1.len() {
18+
y[k..].copy_from_slice(&x1[i..]);
19+
}
20+
if j < x2.len() {
21+
y[k..].copy_from_slice(&x2[j..]);
22+
}
23+
}
24+
25+
fn merge_sort<T: Copy + Ord>(x: &mut [T]) {
26+
let n = x.len();
27+
let m = n / 2;
28+
29+
if n <= 1 {
30+
return;
31+
}
32+
33+
merge_sort(&mut x[0..m]);
34+
merge_sort(&mut x[m..n]);
35+
36+
let mut y: Vec<T> = x.to_vec();
37+
38+
merge(&x[0..m], &x[m..n], &mut y[..]);
39+
40+
x.copy_from_slice(&y);
41+
}
42+
43+
fn main() {
44+
println!("Sort numbers ascending");
45+
let mut numbers = [4, 65, 2, -31, 0, 99, 2, < F438 span class=pl-c1>83, 782, 1];
46+
println!("Before: {:?}", numbers);
47+
merge_sort(&mut numbers);
48+
println!("After: {:?}\n", numbers);
49+
50+
println!("Sort strings alphabetically");
51+
let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"];
52+
println!("Before: {:?}", strings);
53+
merge_sort(&mut strings);
54+
println!("After: {:?}\n", strings);
55+
}

Sorting/countsort/cpp/counting_sort.cpp

Lines changed: 0 additions & 42 deletions
This file was deleted.

Sorting/quickSort/Rust/quick-sort.rs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
pub fn quick_sort<T: Ord>(arr: &mut [T]) {
2+
let len = arr.len();
3+
_quick_sort(arr, 0, (len - 1) as isize);
4+
}
5+
6+
fn _quick_sort<T: Ord>(arr: &mut [T], low: isize, high: isize) {
7+
if low < high {
8+
let p = partition(arr, low, high);
9+
_quick_sort(arr, low, p - 1);
10+
_quick_sort(arr, p + 1, high);
11+
}
12+
}
13+
14+
fn partition<T: Ord>(arr: &mut [T], low: isize, high: isize) -> isize {
15+
let pivot = high as usize;
16+
let mut store_index = low - 1;
17+
let mut last_index = high;
18+
19+
loop {
20+
store_index += 1;
21+
while arr[store_index as usize] < arr[pivot] {
22+
store_index += 1;
23+
}
24+
last_index -= 1;
25+
while last_index >= 0 && arr[last_index as usize] > arr[pivot] {
26+
last_index -= 1;
27+
}
28+
if store_index >= last_index {
29+
break;
30+
} else {
31+
arr.swap(store_index as usize, last_index as usize);
32+
}
33+
}
34+
arr.swap(store_index as usize, pivot as usize);
35+
store_index
36+
}
37+
fn main() {
38+
println!("Sort numbers ascending");
39+
let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1];
40+
println!("Before: {:?}", numbers);
41+
quick_sort(&mut numbers);
42+
println!("After: {:?}\n", numbers);
43+
}

0 commit comments

Comments
 (0)
0