From 72bb7847d63b96ed2de43c72234604eb1280646d Mon Sep 17 00:00:00 2001 From: Vishal Gaur Date: Thu, 1 Oct 2020 09:29:00 +0530 Subject: [PATCH 1/5] Create counting-sort.rs --- Sorting/Counting Sort/Rust/counting-sort.rs | 63 +++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Sorting/Counting Sort/Rust/counting-sort.rs diff --git a/Sorting/Counting Sort/Rust/counting-sort.rs b/Sorting/Counting Sort/Rust/counting-sort.rs new file mode 100644 index 000000000..dd0b01382 --- /dev/null +++ b/Sorting/Counting Sort/Rust/counting-sort.rs @@ -0,0 +1,63 @@ +/// Counting sort + +/// * `arr` - Collection of value to be sorted in place. + +/// * `min` - Lower bound of the integer range. + +/// * `max` - Upper bound of the integer range. + +/// * `key` - Function extracting key witn the integer range from elements. + +pub fn counting_sort(arr: &mut [T], min: usize, max: usize, key: F) +where + F: Fn(&T) -> usize, + T: Clone, +{ + let mut prefix_sums = { + // 1. Initialize the count array with default value 0. + let len = max - min; + let mut count_arr = Vec::with_capacity(len); + count_arr.resize(len, 0); + + // 2. Scan elements to collect counts. + for value in arr.iter() { + count_arr[key(value)] += 1; + } + + // 3. Calculate prefix sum. + count_arr + .into_iter() + .scan(0, |state, x| { + *state += x; + Some(*state - x) + }) + .collect::>() + }; + + // 4. Use prefix sum as index position of output element. + for value in arr.to_vec().iter() { + let index = key(value); + arr[prefix_sums[index]] = value.clone(); + prefix_sums[index] += 1; + } +} + +#[cfg(test)] + +mod base { + use super::*; + fn counting_sort_(arr: &mut [i32]) { + counting_sort(arr, 1, 10, |int| *int as usize); + } + base_cases!(counting_sort_); +} + +#[cfg(test)] + +mod stability { + use super::*; + fn counting_sort_(arr: &mut [(i32, i32)]) { + counting_sort(arr, 1, 10, |t| t.0 as usize); + } + stability_cases!(counting_sort_); +} From aa459d82be7dfbc03c039ce0cb28a882ef2481c3 Mon Sep 17 00:00:00 2001 From: Vishal Gaur Date: Thu, 1 Oct 2020 09:32:08 +0530 Subject: [PATCH 2/5] Create merge-sort.rs --- Sorting/Merge Sort/Rust/merge-sort.rs | 55 +++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 Sorting/Merge Sort/Rust/merge-sort.rs diff --git a/Sorting/Merge Sort/Rust/merge-sort.rs b/Sorting/Merge Sort/Rust/merge-sort.rs new file mode 100644 index 000000000..042367fed --- /dev/null +++ b/Sorting/Merge Sort/Rust/merge-sort.rs @@ -0,0 +1,55 @@ +fn merge(x1: &[T], x2: &[T], y: &mut [T]) { + assert_eq!(x1.len() + x2.len(), y.len()); + let mut i = 0; + let mut j = 0; + let mut k = 0; + while i < x1.len() && j < x2.len() { + if x1[i] < x2[j] { + y[k] = x1[i]; + k += 1; + i += 1; + } else { + y[k] = x2[j]; + k += 1; + j += 1; + } + } + if i < x1.len() { + y[k..].copy_from_slice(&x1[i..]); + } + if j < x2.len() { + y[k..].copy_from_slice(&x2[j..]); + } +} + +fn merge_sort(x: &mut [T]) { + let n = x.len(); + let m = n / 2; + + if n <= 1 { + return; + } + + merge_sort(&mut x[0..m]); + merge_sort(&mut x[m..n]); + + let mut y: Vec = x.to_vec(); + + merge(&x[0..m], &x[m..n], &mut y[..]); + + x.copy_from_slice(&y); +} + +fn main() { + println!("Sort numbers ascending"); + let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]; + println!("Before: {:?}", numbers); + merge_sort(&mut numbers); + println!("After: {:?}\n", numbers); + + println!("Sort strings alphabetically"); + let mut strings = ["beach", "hotel", "airplane", "car", "house", "art"]; + println!("Before: {:?}", strings); + merge_sort(&mut strings); + println!("After: {:?}\n", strings); +} From 179d3dc929e9c652e434ff1728f0255702677c09 Mon Sep 17 00:00:00 2001 From: Vishal Gaur Date: Thu, 1 Oct 2020 09:43:17 +0530 Subject: [PATCH 3/5] Create quick-sort.rs --- Sorting/quickSort/Rust/quick-sort.rs | 43 ++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 Sorting/quickSort/Rust/quick-sort.rs diff --git a/Sorting/quickSort/Rust/quick-sort.rs b/Sorting/quickSort/Rust/quick-sort.rs new file mode 100644 index 000000000..57f429217 --- /dev/null +++ b/Sorting/quickSort/Rust/quick-sort.rs @@ -0,0 +1,43 @@ +pub fn quick_sort(arr: &mut [T]) { + let len = arr.len(); + _quick_sort(arr, 0, (len - 1) as isize); +} + +fn _quick_sort(arr: &mut [T], low: isize, high: isize) { + if low < high { + let p = partition(arr, low, high); + _quick_sort(arr, low, p - 1); + _quick_sort(arr, p + 1, high); + } +} + +fn partition(arr: &mut [T], low: isize, high: isize) -> isize { + let pivot = high as usize; + let mut store_index = low - 1; + let mut last_index = high; + + loop { + store_index += 1; + while arr[store_index as usize] < arr[pivot] { + store_index += 1; + } + last_index -= 1; + while last_index >= 0 && arr[last_index as usize] > arr[pivot] { + last_index -= 1; + } + if store_index >= last_index { + break; + } else { + arr.swap(store_index as usize, last_index as usize); + } + } + arr.swap(store_index as usize, pivot as usize); + store_index +} +fn main() { + println!("Sort numbers ascending"); + let mut numbers = [4, 65, 2, -31, 0, 99, 2, 83, 782, 1]; + println!("Before: {:?}", numbers); + quick_sort(&mut numbers); + println!("After: {:?}\n", numbers); +} From a01a91e40ea468697f6c834b27733ab8345bd8bf Mon Sep 17 00:00:00 2001 From: Vishal Gaur Date: Thu, 1 Oct 2020 09:44:09 +0530 Subject: [PATCH 4/5] Rename Sorting/BubbleSortPHP/bubblesort.php to Sorting/Bubble Sort/PHP/bubble-sort.php --- .../bubblesort.php => Bubble Sort/PHP/bubble-sort.php} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename Sorting/{BubbleSortPHP/bubblesort.php => Bubble Sort/PHP/bubble-sort.php} (99%) diff --git a/Sorting/BubbleSortPHP/bubblesort.php b/Sorting/Bubble Sort/PHP/bubble-sort.php similarity index 99% rename from Sorting/BubbleSortPHP/bubblesort.php rename to Sorting/Bubble Sort/PHP/bubble-sort.php index c79acbd29..3e62f9a45 100644 --- a/Sorting/BubbleSortPHP/bubblesort.php +++ b/Sorting/Bubble Sort/PHP/bubble-sort.php @@ -25,4 +25,4 @@ function print_array($array) { print_array($array); bubble_sort($array); -?> \ No newline at end of file +?> From cbf9a7feecef0c0a231577a4fd6d94f3e00d9971 Mon Sep 17 00:00:00 2001 From: Vishal Gaur Date: Thu, 1 Oct 2020 09:45:23 +0530 Subject: [PATCH 5/5] Delete counting_sort.cpp --- Sorting/countsort/cpp/counting_sort.cpp | 42 ------------------------- 1 file changed, 42 deletions(-) delete mode 100644 Sorting/countsort/cpp/counting_sort.cpp diff --git a/Sorting/countsort/cpp/counting_sort.cpp b/Sorting/countsort/cpp/counting_sort.cpp deleted file mode 100644 index ccc9e9499..000000000 --- a/Sorting/countsort/cpp/counting_sort.cpp +++ /dev/null @@ -1,42 +0,0 @@ -#include - -using namespace std; - -void count_sort(std::vector &v, int mx) -{ - int a[mx + 1]; - memset(a, 0, sizeof a); - for (int i = 0; i < v.size(); i++) - { - a[v[i]]++; - } - - int ptr = 0; - for (int i = 0; i < mx + 1; i++) - { - if (a[i] != 0) - { - while (a[i] != 0) - { - v[ptr++] = i; - a[i]--; - } - } - } -} - -int main(void) -{ - std::vector v; - v.resize(5); - v = {4, 5, 7, 1, 0}; - int mx = 7; - - count_sort(v, mx); - - for (int i = 0; i < v.size(); i++) - { - cout << v[i] << endl; - } - return 0; -} \ No newline at end of file