Skip to content

Commit 389b80c

Browse files
Merge pull request #1 from johannes67890/quicksort
Quicksort
2 parents 9b8cec7 + f352f87 commit 389b80c

File tree

4 files changed

+85
-30
lines changed

4 files changed

+85
-30
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
11
# Sorting-algorithm
2+
23
Sorting algorithms in Rust.
4+
5+
## Table of Contents
6+
7+
- [Install](#install)
8+
- [Usage](#usage)
9+
- [Showcase](#showcase)
10+
- [Benchmarks](#benchmarks)
11+
- [Instructions](#instructions)
12+
- [Supported Curves](#supported-curves)
13+
- [3rd party libraries used](#supported-curves)
14+
15+
## Install
16+
17+
## Usage
18+
19+
## Benchmarks
20+
21+
## Showcase
22+
23+
## Supported Algoritms

benches/sort_benchmark.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
use criterion::{black_box, criterion_group, criterion_main, Criterion};
22
use sorting_algorithm::bubblesort;
3+
use sorting_algorithm::quicksort;
34

45
fn sort_arrays_benchmark(c: &mut Criterion) {
56
let mut arr = black_box([-2,-7,2,10,20,9]);
67

7-
c.bench_function("Bubblesort", |b| b.iter(|| bubblesort(&mut arr)));
8-
8+
c.bench_function("Bubble Sort", |b| b.iter(|| bubblesort(&mut arr)));
9+
c.bench_function("Quick Sort", |b| b.iter(|| quicksort(&mut arr)));
910
}
1011

1112
criterion_group!(benches, sort_arrays_benchmark);

src/lib.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ mod quciksort;
44
pub use bubblesort::bubblesort;
55
pub use quciksort::quicksort;
66

7+
// pub fn generate_random_array(salt: i8, range: i32) -> &[i32]{
8+
// let mut arr = [];
9+
// &arr
10+
// }
11+
12+
713
#[cfg(test)]
814
mod tests {
915
use super::*;
@@ -15,8 +21,8 @@ mod tests {
1521
}
1622
#[test]
1723
fn test_quick_sort() {
18-
let mut array = [10, 7, 8, 9, 1, 5];
19-
let sorted = quicksort(&mut array);
20-
assert_eq!(sorted, &[1, 5, 7, 8, 9, 10]);
24+
let mut arr = [6, 2, 4, 1, 9, -2, 5];
25+
quicksort(&mut arr);
26+
assert_eq!(arr, [-2, 1, 2, 4, 5, 6, 9]);
2127
}
2228
}

src/quciksort.rs

Lines changed: 52 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,62 @@
1-
pub fn quicksort(array: &mut [i32]) -> &[i32] {
2-
let low: isize = 0;
3-
let high: isize = (array.len() - 1) as isize;
1+
2+
/// # Quicksort
3+
///
4+
/// The Quicksort algoritm is a Divide and Conquer algorithm.
5+
/// Quicksort selects a `pivot` element, and takes the other elements smaller or equal on one side and bigger on the other side.
6+
///
7+
/// # Ilustration
8+
/// `arr = [ 8, 5, 9, 2, 7 ]` (pivot is the last element, in this case 7).
9+
///
10+
/// <-- `less or equal` `bigger than` -->
11+
///
12+
/// [ 8, 5, 9, 2, 7 ]
13+
/// |
14+
/// / ------------- 7 ----------------\
15+
/// [ 5, 2 ] [ 8, 9 ]
16+
/// | |
17+
/// [ ] -- 2 -- [5] [8] -- 9 -- [ ]
18+
/// | | <---- `Now we join them back together`
19+
/// \----- [ 2, 5, 7, 8, 9 ] ---- /
20+
/// (Sorted array)
21+
///
22+
pub fn quicksort<T: Ord>(array: &mut [T]) { // Takes mut array of type 'T'.
23+
let low: isize = 0; // Index '0' of array
24+
let high: isize = (array.len() - 1) as isize; // Highest index of array
425

5-
sort(array, low, high);
26+
sort(array, low, high);
627

28+
}
729

8-
fn sort(array: &mut [i32], low: isize, high: isize) -> &[i32]{
9-
if low < high {
10-
let pi: isize = temp(array, low, high);
1130

12-
sort(array, low, pi - 1);
13-
sort(array, (pi + 1) as isize, high);
14-
}
15-
array
16-
}
31+
fn sort<T: Ord>(arr: &mut [T], low: isize, high: isize){
32+
if low < high {
33+
let pivot_element: isize = temp(arr, low, high); // pivot element (always the element of the highest index, in the whole or sub array)
1734

18-
fn temp(array: &mut [i32], low: isize, high: isize) -> isize {
19-
let temp = array[high as usize];
20-
let mut i = low - 1;
21-
22-
for j in 0..high {
23-
if array[j as usize] < temp {
35+
sort(arr, low, pivot_element - 1); // Before pi
36+
sort(arr, pivot_element + 1, high); // After pi
37+
}
38+
39+
fn temp<T: Ord>(arr: &mut [T], low: isize, high: isize) -> isize {
40+
let pivot = high as usize; // Pivot element
41+
let mut i = low - 1;
42+
let mut j = high;
43+
44+
loop {
45+
i += 1;
46+
while arr[i as usize] < arr[pivot] {
2447
i += 1;
25-
26-
array.swap(i as usize, j as usize);
27-
48+
}
49+
j -= 1;
50+
while j >= 0 && arr[j as usize] > arr[pivot] {
51+
j -= 1;
52+
}
53+
if i >= j { // If current element is smaller than pivot element.
54+
break;
55+
} else {
56+
arr.swap(i as usize, j as usize);
2857
}
2958
}
30-
array.swap((i + 1) as usize, high as usize);
31-
i + 1
59+
arr.swap(i as usize, pivot as usize);
60+
i
3261
}
33-
34-
array
3562
}

0 commit comments

Comments
 (0)