From 860acf0fdc584144255be31cfecb6da7d87d7c73 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20H=C3=B6nes?= Date: Sun, 6 Nov 2016 23:48:18 +0100 Subject: [PATCH] Aufgaben2_choenes_tkruemmel --- aufgaben/sheet2/task1/prime.rs | 22 ++++++++++++++++++++++ aufgaben/sheet2/task2/sort.rs | 30 ++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 2 deletions(-) diff --git a/aufgaben/sheet2/task1/prime.rs b/aufgaben/sheet2/task1/prime.rs index b2d0aa0..aebe2fb 100755 --- a/aufgaben/sheet2/task1/prime.rs +++ b/aufgaben/sheet2/task1/prime.rs @@ -2,8 +2,30 @@ fn main() { + println!("1"); + for prime in 2..21 { + if is_prime(prime){ + println!("{}*", prime); + } else { + println!("{}", prime); + } + } + } +fn is_prime(prime: i32) -> bool { + let mut iterator = 2; + // testet ob ohne Rest teilbar, wenn nicht = Primzahl + while iterator <= prime / 2 { + if prime % iterator == 0 { + return false; + } + iterator += 1; + } + true +} + + #[test] fn small_primes() { assert!(is_prime(2)); diff --git a/aufgaben/sheet2/task2/sort.rs b/aufgaben/sheet2/task2/sort.rs index e582fc7..faa9970 100755 --- a/aufgaben/sheet2/task2/sort.rs +++ b/aufgaben/sheet2/task2/sort.rs @@ -2,11 +2,36 @@ fn main() { let mut arr = [61, 21, 27, 79, 57, 60, 46, 42, 27, 92, 66, 26]; sort(&mut arr); - // TODO: print `arr` + println!("{:?}", arr); } -// TODO: write `sort()` function +fn sort(arr: &mut [i32]) { + let mut min; + let mut temp = 0; + let mut pointer = 0; + let length = arr.len(); + + for counter in 0..length { + min = arr[counter]; + // läuft unsortierten teil des arrays ab + for counter2 in 0..length { + let iter = counter2 + counter; + if iter > length - 1{ + break; + } + + if arr[iter] <= min { + min = arr[iter]; + temp = arr[counter]; + pointer = iter; + } + // tausche min an die richtige Stelle + arr[counter] = min; + arr[pointer] = temp; + } + } +} #[test] fn sort_array() { @@ -24,3 +49,4 @@ fn sort_array() { 90, 91, 92, 94, 95, 96, 98, ] as &[u64]); } +