Skip to content

Commit 53cf278

Browse files
committed
Merge ready, with README and comments
1 parent 8cce2b4 commit 53cf278

File tree

10 files changed

+183
-30
lines changed

10 files changed

+183
-30
lines changed

.vscode/settings.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"rust-analyzer.linkedProjects": [
3+
".\\Cargo.toml"
4+
]
5+
}

README.md

Lines changed: 82 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,99 @@
11
# Sorting-algorithm
22

3-
Sorting algorithms in Rust.
3+
Tested and Benchmarked Sorting algorithms in Rust.
44

55
## Table of Contents
66

77
- [Install](#install)
88
- [Usage](#usage)
99
- [Showcase](#showcase)
10+
- [Supported Algoritms](#supported-algoritms)
1011
- [Benchmarks](#benchmarks)
11-
- [Instructions](#instructions)
12-
- [Supported Curves](#supported-curves)
13-
- [3rd party libraries used](#supported-curves)
1412

1513
## Install
16-
14+
Clone and install this Github repository
15+
```git
16+
git clone https://github.com/johannes67890/Sorting-algorithm.git
17+
```
18+
```
19+
cargo install
20+
```
1721
## Usage
22+
```js
23+
// Run tests on functions
24+
> npm test
1825

19-
## Benchmarks
26+
// Run benchmarks on functions
27+
> npm bench
28+
```
2029

2130
## Showcase
31+
```rust
32+
/// Imported algorithm modules (See `Supported Algoritms`)
33+
mod bubble_sort;
34+
mod quick_sort;
35+
36+
fn main(){
37+
let mut arr = [6, 2, 4, 1, 9, -2, 5]; // array to sort
2238

39+
// Bubble sort
40+
bubble_sort::bubblesort(&mut arr); // sort the array
41+
println!("Bubble sort: {:?}", arr); // print the sorted array
42+
43+
// Quick sort
44+
quick_sort::quicksort(&mut arr); // sort the array
45+
println!("Quick sort: {:?}", arr); // print the sorted array
46+
}
47+
```
2348
## Supported Algoritms
49+
- Bubble Sort
50+
- Insertion Sort
51+
- Merge Sort
52+
- Quick Sort
53+
- Shell Sort
54+
## Benchmarks
55+
Line Chart of all supported algoritms from size 1 to 1000 element arrays
56+
![Benchmark Image](./assets/1to1000SizeBench.PNG)
57+
## Individual Benchmarks (Size 100)
58+
Benchmarks of all sorting algorithms with size of array of 100 elements.
59+
#### Bubble Sort
60+
```
61+
Sorting Algorithms/Bubble Sort/100
62+
time: [8.1996 µs 8.2539 µs 8.3318 µs]
63+
Found 10 outliers among 100 measurements (10.00%)
64+
1 (1.00%) high mild
65+
9 (9.00%) high severe
66+
```
67+
### Merge Sort
68+
```
69+
Sorting Algorithms/Merge Sort/100
70+
time: [12.033 µs 12.143 µs 12.277 µs]
71+
Found 17 outliers among 100 measurements (17.00%)
72+
4 (4.00%) high mild
73+
13 (13.00%) high severe
74+
```
75+
### Insertion Sort
76+
```
77+
Sorting Algorithms/Insertion Sort/100
78+
time: [3.0122 µs 3.0183 µs 3.0256 µs]
79+
Found 7 outliers among 100 measurements (7.00%)
80+
3 (3.00%) high mild
81+
4 (4.00%) high severe
82+
```
83+
### Quick Sort
84+
```
85+
Sorting Algorithms/Quick Sort/100
86+
time: [2.2028 µs 2.2116 µs 2.2222 µs]
87+
Found 10 outliers among 100 measurements (10.00%)
88+
5 (5.00%) high mild
89+
5 (5.00%) high severe
90+
```
91+
### Shell Sort
92+
```
93+
Sorting Algorithms/Shell Sort/100
94+
time: [2.6537 µs 2.6644 µs 2.6769 µs]
95+
Found 10 outliers among 100 measurements (10.00%)
96+
6 (6.00%) high mild
97+
4 (4.00%) high severe
98+
```
99+
# [Back to the top](#sorting-algorithm)

assets/1to1000SizeBench.PNG

41.7 KB
Loading

benches/sort_benchmark.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ use sorting_algorithm::{bubblesort, insertionsort, mergesort, quicksort, shellso
55
fn sorting_benchmarks(c: &mut Criterion) {
66
let mut group = c.benchmark_group("Sorting Algorithms");
77

8-
for i in [1,5,10, 25,50,75, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] { // size of array
8+
for i in [1,5,10, 25,50,75, 100, 200, 300, 400, 500, 600, 700, 800, 900, 1000] { // size of array
99
let mut rng = rand::thread_rng();
1010
let range = Uniform::new(0, 100); // range of random numbers
1111

1212
// Bubble sort benchmark (Time complexity: O(n^2))
1313
group.bench_function(BenchmarkId::new("Bubble Sort", i), |b| {
1414
b.iter_batched_ref(
15-
|| -> Vec<usize> { (0..i).map(|_| rng.sample(&range)).collect() },
16-
|v| bubblesort( v),
17-
BatchSize::SmallInput,
15+
|| -> Vec<usize> { (0..i).map(|_| rng.sample(&range)).collect() }, // creates a vector of random numbers
16+
|v| bubblesort( v), // v is the vector/array to be sorted
17+
BatchSize::SmallInput, // BatchSize::SmallInput is the number of times the function is run
1818
)
1919
});
2020
// Merge sort benchmark (Time complexity: O(nlogn))

src/bubble_sort.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
1+
/// Bubble sort
2+
///
3+
/// Bubble sort swaps each pair of adjacent items if they are in the wrong order.
4+
/// repeat until no swaps are needed.
5+
///
6+
/// # Time complexity
7+
/// * Best case: O(n)
8+
/// * Average case: O(n^2)
9+
/// * Worst case: O(n^2)
10+
///
11+
/// # Ilustration
12+
/// ```` ignore
13+
/// arr = [ 8, 5, 9, 2, 7 ]
14+
///
15+
/// [ 8, 5, 9, 2, 7 ] -> [ 5, 8, 9, 2, 7 ] -> [ 5, 8, 9, 2, 7 ] -> [ 2, 5, 8, 9, 7 ] -> [ 2, 5, 7, 8, 9 ]
16+
/// ````
117
pub fn bubblesort<T: Ord>(arr: &mut [T]) {
2-
let len = arr.len();
3-
for i in 0..len {
4-
for j in 0..len - i - 1 {
5-
if arr[j] > arr[j + 1] {
6-
arr.swap(j, j + 1);
7-
}
18+
let len = arr.len();
19+
for i in 0..len {
20+
for j in 0..len - i - 1 {
21+
if arr[j] > arr[j + 1] { // swap if the current value is greater than the next value
22+
arr.swap(j, j + 1);
823
}
924
}
1025
}
26+
}
1127

1228

src/insertion_sort.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,17 @@
22
///
33
/// The Insertion sort algorithm is a simple sorting algorithm that builds the final sorted array (or list) one item at a time.
44
///
5+
/// # Time complexity
6+
/// * Best case: O(n)
7+
/// * Average case: O(n^2)
8+
/// * Worst case: O(n^2)
9+
///
510
/// # Ilustration
6-
/// `arr = [ 8, 5, 9, 2, 7 ]`
11+
/// ```` ignore
12+
/// arr = [ 8, 5, 9, 2, 7 ]
713
///
814
/// [ 8, 5, 9, 2, 7 ] -> [ 5, 8, 9, 2, 7 ] -> [ 5, 8, 9, 2, 7 ] -> [ 2, 5, 8, 9, 7 ] -> [ 2, 5, 7, 8, 9 ]
9-
///
15+
/// ````
1016
pub fn insertionsort<T: Ord>(array: &mut [T]) {
1117
let mut n = array.len();
1218
for i in 1..n {

src/lib.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,12 @@ pub use quick_sort::quicksort;
99
pub use insertion_sort::insertionsort;
1010
pub use merge_sort::mergesort;
1111
pub use shell_sort::shellsort;
12-
12+
///
13+
/// Test sorting algorithms
14+
///
1315
#[cfg(test)]
1416
mod tests {
1517
use super::*;
16-
///
17-
/// Test sorting algorithms
18-
///
19-
2018
#[test]
2119
fn test_bubble_sort() {
2220
let mut arr = [6, 2, 4, 1, 9, -2, 5];

src/merge_sort.rs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,45 @@
1+
/// Merge Sort
2+
///
3+
/// Merge sort is a divide and conquer algorithm.
4+
///
5+
/// The pivot point is the middle index of array. It is calculated by `(left index + right index) / 2`.
6+
/// The array is divided into two subarrays, left and right.
7+
/// The left subarray is sorted by recursively calling the merge sort function.
8+
/// The right subarray is sorted by recursively calling the merge sort function, after the left subarray is sorted.
9+
/// At the end, the two subarrays are merged into one sorted array.
10+
///
11+
/// # Time complexity
12+
/// * Best case: O(n log n)
13+
/// * Average case: O(n log n)
14+
/// * Worst case: O(n log n)
15+
///
16+
/// # Ilustration
17+
/// ``` ignore
18+
/// arr = [ 8, 5, 9, 2, 7 ]`
19+
/// if left index < right index -> pivot point = (left index + right index) / 2
20+
///
21+
/// <-- Left Index Right Index -->
22+
///
23+
/// [ 8, 5, 9, 2, 7 ] --> pivot point = 2
24+
/// |
25+
/// / -------------------------------\
26+
/// [ 8, 5, 9 ] [ 2, 7 ]
27+
/// | |
28+
/// [ 8, 5 ] ------ [9] [2] ----- [7]
29+
/// | |
30+
/// [ 8 ] -- [ 5 ] -- [ ] | -> sort and merge the two subarrays into one sorted array
31+
/// | |
32+
/// [ 5 ] -- [ 8 ] -- [ 9 ] [ 2 ] -- [ 7 ]
33+
/// | |
34+
/// [ 5, 8 ] -- [ 9 ] [ 2, 7 ]
35+
/// | |
36+
/// [ 5, 8, 9 ] |
37+
/// | |
38+
/// \----- [ 2, 5, 7, 8, 9 ] ---- /
39+
///
40+
/// ```
41+
/// [See detailed illustration of Merge sort in action](https://media.geeksforgeeks.org/wp-content/uploads/20220722205737/MergeSortTutorial.png)
42+
///
143
pub fn mergesort<T: Ord + Clone>(arr: &mut [T]) {
244
sort(arr, 0, arr.len() - 1);
345
}

src/quick_sort.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
/// The Quicksort algoritm is a Divide and Conquer algorithm.
55
/// Quicksort selects a `pivot` element, and takes the other elements smaller or equal on one side and bigger on the other side.
66
///
7+
/// # Time complexity
8+
/// * Best case: O(n log(n))
9+
/// * Average case: O(n log(n))
10+
/// * Worst case: O(n^2)
11+
///
712
/// # Ilustration
813
/// ``` ignore
914
/// arr = [ 8, 5, 9, 2, 7 ]` (pivot is the last element, in this case 7).

src/shell_sort.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
11
/// Shell sort
22
///
3+
/// # Algorithm
4+
/// Shell sort is a variation of insertion sort.
5+
/// Shell sort uses a gap value to create sublists.
6+
/// The gap value starts with a larger value and gets smaller each time through the loop until it becomes 1.
7+
/// Once the gap is 1, the list is then sorted using insertion sort.
8+
///
9+
/// # Time complexity
10+
/// * Best case: O(n)
11+
/// * Average case: O(n^2)
12+
/// * Worst case: O(n^2)
13+
///
314
/// # Ilustration
415
/// ``` ignore
516
/// arr = [ 8, 5, 9, 2, 7 ]
@@ -12,13 +23,7 @@
1223
///
1324
/// [ 5, 8, 2, 7, 9 ] -> [ 2, 5, 7, 8, 9 ]
1425
/// ```
15-
///
16-
/// # Complexity
17-
///
18-
/// | Best | Average | Worst |
19-
/// |------|---------|-------|
20-
/// | O(n) | O(n^2) | O(n^2)|
21-
///
26+
2227
pub fn shellsort<T: Ord + Copy>(arr: &mut [T]) {
2328
let len = arr.len();
2429
let mut gap = len / 2;

0 commit comments

Comments
 (0)