Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,23 @@
# Sorting-algorithm

Sorting algorithms in Rust.

## Table of Contents

- [Install](#install)
- [Usage](#usage)
- [Showcase](#showcase)
- [Benchmarks](#benchmarks)
- [Instructions](#instructions)
- [Supported Curves](#supported-curves)
- [3rd party libraries used](#supported-curves)

## Install

## Usage

## Benchmarks

## Showcase

## Supported Algoritms
5 changes: 3 additions & 2 deletions benches/sort_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use sorting_algorithm::bubblesort;
use sorting_algorithm::quicksort;

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

c.bench_function("Bubblesort", |b| b.iter(|| bubblesort(&mut arr)));

c.bench_function("Bubble Sort", |b| b.iter(|| bubblesort(&mut arr)));
c.bench_function("Quick Sort", |b| b.iter(|| quicksort(&mut arr)));
}

criterion_group!(benches, sort_arrays_benchmark);
Expand Down
12 changes: 9 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ mod quciksort;
pub use bubblesort::bubblesort;
pub use quciksort::quicksort;

// pub fn generate_random_array(salt: i8, range: i32) -> &[i32]{
// let mut arr = [];
// &arr
// }


#[cfg(test)]
mod tests {
use super::*;
Expand All @@ -15,8 +21,8 @@ mod tests {
}
#[test]
fn test_quick_sort() {
let mut array = [10, 7, 8, 9, 1, 5];
let sorted = quicksort(&mut array);
assert_eq!(sorted, &[1, 5, 7, 8, 9, 10]);
let mut arr = [6, 2, 4, 1, 9, -2, 5];
quicksort(&mut arr);
assert_eq!(arr, [-2, 1, 2, 4, 5, 6, 9]);
}
}
77 changes: 52 additions & 25 deletions src/quciksort.rs
Original file line number Diff line number Diff line change
@@ -1,35 +1,62 @@
pub fn quicksort(array: &mut [i32]) -> &[i32] {
let low: isize = 0;
let high: isize = (array.len() - 1) as isize;

/// # Quicksort
///
/// The Quicksort algoritm is a Divide and Conquer algorithm.
/// Quicksort selects a `pivot` element, and takes the other elements smaller or equal on one side and bigger on the other side.
///
/// # Ilustration
/// `arr = [ 8, 5, 9, 2, 7 ]` (pivot is the last element, in this case 7).
///
/// <-- `less or equal` `bigger than` -->
///
/// [ 8, 5, 9, 2, 7 ]
/// |
/// / ------------- 7 ----------------\
/// [ 5, 2 ] [ 8, 9 ]
/// | |
/// [ ] -- 2 -- [5] [8] -- 9 -- [ ]
/// | | <---- `Now we join them back together`
/// \----- [ 2, 5, 7, 8, 9 ] ---- /
/// (Sorted array)
///
pub fn quicksort<T: Ord>(array: &mut [T]) { // Takes mut array of type 'T'.
let low: isize = 0; // Index '0' of array
let high: isize = (array.len() - 1) as isize; // Highest index of array

sort(array, low, high);
sort(array, low, high);

}

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

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

fn temp(array: &mut [i32], low: isize, high: isize) -> isize {
let temp = array[high as usize];
let mut i = low - 1;

for j in 0..high {
if array[j as usize] < temp {
sort(arr, low, pivot_element - 1); // Before pi
sort(arr, pivot_element + 1, high); // After pi
}

fn temp<T: Ord>(arr: &mut [T], low: isize, high: isize) -> isize {
let pivot = high as usize; // Pivot element
let mut i = low - 1;
let mut j = high;

loop {
i += 1;
while arr[i as usize] < arr[pivot] {
i += 1;

array.swap(i as usize, j as usize);

}
j -= 1;
while j >= 0 && arr[j as usize] > arr[pivot] {
j -= 1;
}
if i >= j { // If current element is smaller than pivot element.
break;
} else {
arr.swap(i as usize, j as usize);
}
}
array.swap((i + 1) as usize, high as usize);
i + 1
arr.swap(i as usize, pivot as usize);
i
}

array
}