Compare commits
16 commits
Author | SHA1 | Date | |
---|---|---|---|
6f0ef241f5 | |||
ce7b5465f5 | |||
48e994a4e9 | |||
5283bc531c | |||
69a5b4b312 | |||
cb03ff5125 | |||
198e154495 | |||
c370cc4486 | |||
b607608b85 | |||
15d253d49c | |||
9f847fe038 | |||
6659387d40 | |||
62f1b27dff | |||
796ad91016 | |||
df06ce02c9 | |||
451101c7ae |
5
.drone.yml
Normal file
5
.drone.yml
Normal file
|
@ -0,0 +1,5 @@
|
|||
pipeline:
|
||||
build:
|
||||
image: rust
|
||||
commands:
|
||||
- cargo run
|
47
CHANGELOG.md
47
CHANGELOG.md
|
@ -1,3 +1,50 @@
|
|||
<a name="v0.5.1"></a>
|
||||
### v0.5.1 (2017-09-15)
|
||||
|
||||
This release only is the addition of CI for test purposes.
|
||||
|
||||
|
||||
<a name="v0.5.0"></a>
|
||||
## v0.5.0 (2017-08-29)
|
||||
|
||||
|
||||
#### Features
|
||||
|
||||
* add insertionsort ([cb03ff51](cb03ff51))
|
||||
* demo all algorithms ([198e1544](198e1544), closes [#2](2) (and also [#1](1)))
|
||||
|
||||
|
||||
|
||||
<a name="v0.4.0"></a>
|
||||
## v0.4.0 (2017-08-27)
|
||||
|
||||
|
||||
#### Features
|
||||
|
||||
* add quicksort ([b607608b](b607608b))
|
||||
|
||||
|
||||
|
||||
<a name="v0.3.0"></a>
|
||||
## v0.3.0 (2017-08-27)
|
||||
|
||||
|
||||
#### Features
|
||||
|
||||
* add insertionsort ([6659387d](6659387d))
|
||||
|
||||
|
||||
|
||||
<a name="v0.2.0"></a>
|
||||
## v0.2.0 (2017-08-27)
|
||||
|
||||
|
||||
#### Features
|
||||
|
||||
* add mergesort ([df06ce02](df06ce02))
|
||||
|
||||
|
||||
|
||||
<a name="v0.1.0"></a>
|
||||
## v0.1.0 (2017-08-27)
|
||||
|
||||
|
|
4
Cargo.lock
generated
4
Cargo.lock
generated
|
@ -1,4 +1,4 @@
|
|||
[root]
|
||||
name = "sorting"
|
||||
version = "0.1.0"
|
||||
name = "algo"
|
||||
version = "0.5.0"
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
[package]
|
||||
name = "sorting"
|
||||
version = "0.1.0"
|
||||
name = "algo"
|
||||
version = "0.5.1"
|
||||
authors = ["Jan Christian Grünhage <jan.christian@gruenhage.xyz>"]
|
||||
|
||||
[dependencies]
|
||||
|
|
3
README.md
Normal file
3
README.md
Normal file
|
@ -0,0 +1,3 @@
|
|||
Just some sorting algorithm implementations as practice for an exam. If they are useful for you, feel free to use them, under the included license.
|
||||
|
||||
[![Build Status](https://drone.jcg.re/api/badges/jcgruenhage/algo/status.svg)](https://drone.jcg.re/jcgruenhage/algo)
|
|
@ -1,6 +0,0 @@
|
|||
pub trait Algorithm {
|
||||
fn sort(vector: Vec<i32>) -> Vec<i32>;
|
||||
}
|
||||
|
||||
|
||||
pub mod bubblesort;
|
86
src/main.rs
86
src/main.rs
|
@ -1,10 +1,82 @@
|
|||
mod algorithms;
|
||||
mod sorting;
|
||||
|
||||
use algorithms::bubblesort::Bubblesort;
|
||||
use algorithms::Algorithm;
|
||||
use sorting::*;
|
||||
use sorting::Algorithm;
|
||||
|
||||
fn main() {
|
||||
let unsorted : Vec<i32> = vec![2498,29687,24,5,94545,8,51,152,48,871,5];
|
||||
let sorted = Bubblesort::sort(unsorted);
|
||||
println!("{:?}", sorted);
|
||||
}
|
||||
demo_sorting();
|
||||
}
|
||||
|
||||
fn demo_sorting() {
|
||||
let unsorted: Vec<i32> = vec![
|
||||
2498,
|
||||
29687,
|
||||
24,
|
||||
94545,
|
||||
8,
|
||||
51,
|
||||
152,
|
||||
871,
|
||||
5,
|
||||
6557,
|
||||
216,
|
||||
75986,
|
||||
48,
|
||||
526,
|
||||
15,
|
||||
6,
|
||||
78,
|
||||
];
|
||||
|
||||
println!("Unsorted array: {:?}", unsorted);
|
||||
println!(
|
||||
"Bubblesort : {:?}",
|
||||
match bubble::Sort::sort(unsorted.clone()) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
println!("Something went wrong! {}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
println!(
|
||||
"Selectionsort : {:?}",
|
||||
match selection::Sort::sort(unsorted.clone()) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
println!("Something went wrong! {}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
println!(
|
||||
"Insertionsort : {:?}",
|
||||
match insertion::Sort::sort(unsorted.clone()) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
println!("Something went wrong! {}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
println!(
|
||||
"Mergesort : {:?}",
|
||||
match merge::Sort::sort(unsorted.clone()) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
println!("Something went wrong! {}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
println!(
|
||||
"Quicksort : {:?}",
|
||||
match quick::Sort::sort(unsorted.clone()) {
|
||||
Ok(v) => v,
|
||||
Err(e) => {
|
||||
println!("Something went wrong! {}", e);
|
||||
return;
|
||||
}
|
||||
}
|
||||
);
|
||||
}
|
||||
|
|
|
@ -1,9 +1,9 @@
|
|||
use algorithms::Algorithm;
|
||||
use sorting::Algorithm;
|
||||
|
||||
pub struct Bubblesort;
|
||||
pub struct Sort;
|
||||
|
||||
impl Algorithm for Bubblesort {
|
||||
fn sort(mut vector: Vec<i32>) -> Vec<i32> {
|
||||
impl Algorithm for Sort {
|
||||
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||
for i in 0..(vector.len()) {
|
||||
for j in 0..(vector.len() - i - 1) {
|
||||
if vector[j] > vector[j + 1] {
|
||||
|
@ -13,6 +13,6 @@ impl Algorithm for Bubblesort {
|
|||
}
|
||||
}
|
||||
}
|
||||
return vector;
|
||||
return Ok(vector);
|
||||
}
|
||||
}
|
||||
}
|
17
src/sorting/insertion.rs
Normal file
17
src/sorting/insertion.rs
Normal file
|
@ -0,0 +1,17 @@
|
|||
use sorting::*;
|
||||
|
||||
pub struct Sort;
|
||||
|
||||
impl Algorithm for Sort {
|
||||
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||
for i in 0..vector.len() {
|
||||
for j in 0..(i) {
|
||||
if vector[j] > vector[i] {
|
||||
vector = rotate(vector, j, i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
return Ok(vector);
|
||||
}
|
||||
}
|
60
src/sorting/merge.rs
Normal file
60
src/sorting/merge.rs
Normal file
|
@ -0,0 +1,60 @@
|
|||
use sorting::Algorithm;
|
||||
|
||||
pub struct Sort;
|
||||
|
||||
impl Algorithm for Sort {
|
||||
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||
if vector.len() == 1 {
|
||||
return Ok(vector);
|
||||
} else {
|
||||
let mut second: Vec<i32> = Vec::new();
|
||||
let length = vector.len();
|
||||
for _ in 0..length / 2 {
|
||||
second.push(match vector.pop() {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
return Err("Error during splitting the vector.");
|
||||
}
|
||||
});
|
||||
}
|
||||
let first = match Sort::sort(vector) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
second = match Sort::sort(second) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
let mut result: Vec<i32> = Vec::new();
|
||||
|
||||
let first_length = first.len();
|
||||
let second_length = second.len();
|
||||
|
||||
let mut first_index = 0;
|
||||
let mut second_index = 0;
|
||||
|
||||
while first_index < first_length && second_index < second_length {
|
||||
if first[first_index] < second[second_index] {
|
||||
result.push(first[first_index]);
|
||||
first_index += 1;
|
||||
} else {
|
||||
result.push(second[second_index]);
|
||||
second_index += 1;
|
||||
}
|
||||
}
|
||||
|
||||
while first_index < first_length {
|
||||
result.push(first[first_index]);
|
||||
first_index += 1;
|
||||
}
|
||||
|
||||
while second_index < second_length {
|
||||
result.push(second[second_index]);
|
||||
second_index += 1;
|
||||
}
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
||||
}
|
21
src/sorting/mod.rs
Normal file
21
src/sorting/mod.rs
Normal file
|
@ -0,0 +1,21 @@
|
|||
pub trait Algorithm {
|
||||
fn sort(vector: Vec<i32>) -> Result<Vec<i32>, &'static str>;
|
||||
}
|
||||
|
||||
|
||||
pub mod bubble;
|
||||
pub mod merge;
|
||||
pub mod selection;
|
||||
pub mod quick;
|
||||
pub mod insertion;
|
||||
|
||||
pub fn rotate(mut vector: Vec<i32>, from: usize, to: usize) -> Vec<i32> {
|
||||
if from != to {
|
||||
let to_value = vector[to];
|
||||
for j in (from..(to)).rev() {
|
||||
vector[j + 1] = vector[j];
|
||||
}
|
||||
vector[from] = to_value;
|
||||
}
|
||||
return vector;
|
||||
}
|
59
src/sorting/quick.rs
Normal file
59
src/sorting/quick.rs
Normal file
|
@ -0,0 +1,59 @@
|
|||
use sorting::Algorithm;
|
||||
|
||||
pub struct Sort;
|
||||
|
||||
impl Algorithm for Sort {
|
||||
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||
let pivot = match vector.pop() {
|
||||
Some(v) => v,
|
||||
None => return Ok(Vec::new()),
|
||||
};
|
||||
let mut pivots: Vec<i32> = Vec::new();
|
||||
pivots.push(pivot);
|
||||
|
||||
let mut smaller: Vec<i32> = Vec::new();
|
||||
let mut larger: Vec<i32> = Vec::new();
|
||||
|
||||
while vector.len() != 0 {
|
||||
let element = match vector.pop() {
|
||||
Some(v) => v,
|
||||
None => {
|
||||
return Err(
|
||||
"Vector had a length not equal to 0, but still had no more element in it. This point should not be reachable.",
|
||||
)
|
||||
}
|
||||
};
|
||||
if element < pivot {
|
||||
smaller.push(element);
|
||||
} else if element > pivot {
|
||||
larger.push(element);
|
||||
} else {
|
||||
pivots.push(element);
|
||||
}
|
||||
}
|
||||
|
||||
let mut result: Vec<i32> = Vec::new();
|
||||
|
||||
for v in match Sort::sort(smaller) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
{
|
||||
result.push(v);
|
||||
}
|
||||
|
||||
for v in pivots {
|
||||
result.push(v);
|
||||
}
|
||||
|
||||
for v in match Sort::sort(larger) {
|
||||
Ok(v) => v,
|
||||
Err(e) => return Err(e),
|
||||
}
|
||||
{
|
||||
result.push(v);
|
||||
}
|
||||
|
||||
return Ok(result);
|
||||
}
|
||||
}
|
18
src/sorting/selection.rs
Normal file
18
src/sorting/selection.rs
Normal file
|
@ -0,0 +1,18 @@
|
|||
use sorting::*;
|
||||
|
||||
pub struct Sort;
|
||||
|
||||
impl Algorithm for Sort {
|
||||
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||
for i in 0..vector.len() {
|
||||
let mut smallest_index = i;
|
||||
for j in i..vector.len() {
|
||||
if vector[j] < vector[smallest_index] {
|
||||
smallest_index = j;
|
||||
}
|
||||
}
|
||||
vector = rotate(vector, i, smallest_index);
|
||||
}
|
||||
return Ok(vector);
|
||||
}
|
||||
}
|
9
src/sorting/template.rs
Normal file
9
src/sorting/template.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
use sorting::Algorithm;
|
||||
|
||||
pub struct Sort;
|
||||
|
||||
impl Algorithm for Sort {
|
||||
fn sort(mut vector: Vec<i32>) -> Result<Vec<i32>, &'static str> {
|
||||
return Mergesort::sort(vector);
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue