From aadd8f8725ce8e4debbdd8203793550219749850 Mon Sep 17 00:00:00 2001 From: Florian Sylvain Date: Sat, 3 Jul 2021 00:52:42 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=89=20-=20First=20commit=20!?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + Cargo.lock | 85 +++++++++++++++++++++++++++++++++++ Cargo.toml | 9 ++++ src/main.rs | 125 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 220 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..be9d683 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,85 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "cfg-if" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" + +[[package]] +name = "dichotomie" +version = "0.1.0" +dependencies = [ + "rand", +] + +[[package]] +name = "getrandom" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7fcd999463524c52659517fe2cea98493cfe485d10565e7b0fb07dbba7ad2753" +dependencies = [ + "cfg-if", + "libc", + "wasi", +] + +[[package]] +name = "libc" +version = "0.2.97" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6" + +[[package]] +name = "ppv-lite86" +version = "0.2.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac74c624d6b2d21f425f752262f42188365d7b8ff1aff74c82e45136510a4857" + +[[package]] +name = "rand" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2e7573632e6454cf6b99d7aac4ccca54be06da05aca2ef7423d22d27d4d4bcd8" +dependencies = [ + "libc", + "rand_chacha", + "rand_core", + "rand_hc", +] + +[[package]] +name = "rand_chacha" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88" +dependencies = [ + "ppv-lite86", + "rand_core", +] + +[[package]] +name = "rand_core" +version = "0.6.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d34f1408f55294453790c48b2f1ebbb1c5b4b7563eb1f418bcfcfdbb06ebb4e7" +dependencies = [ + "getrandom", +] + +[[package]] +name = "rand_hc" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d51e9f596de227fda2ea6c84607f5558e196eeaf43c986b724ba4fb8fdf497e7" +dependencies = [ + "rand_core", +] + +[[package]] +name = "wasi" +version = "0.10.2+wasi-snapshot-preview1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..101d869 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "dichotomie" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +rand = "0.8.4" \ No newline at end of file diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..93946e8 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,125 @@ +use rand::Rng; +use std::collections::HashSet; +use std::io; +// use std::time::Instant; + +fn main() { + // Generation d'une liste contenant 255 entiers aleatoire uniques + + let mut array: [i32; 255] = [0; 255]; + let mut hashset: HashSet = HashSet::new(); + + // let now = Instant::now(); + + // Array way + // + // for i in 1..255 { + // let rd = loop { + // let x = rand::thread_rng().gen_range(1..1001); + // if !(array.contains(&x)) { + // break x; + // } + // }; + // array[i] = rd; + // } + // + // Temps d'execution moyen : 1,5 ms + + // HashMap way + // + let mut k: usize = 0; + let mut hash_size: i16 = 0; + while hash_size != 255 { + hashset.insert(rand::thread_rng().gen_range(1..1001)); + hash_size = hashset.len() as i16; + } + for nb in hashset.iter() { + array[k] = *nb; + k += 1; + } + // + // Temps d'execution moyen : 500 µs + + // let new_now = Instant::now(); + // println!("Temps d'execution: {:?}", new_now.duration_since(now)); + + // Trier la liste d'entiers + + array.sort(); + + // Demande d'un entier compris entre minimum et maximum de la liste a l'utilisateur + + let mut number_str = String::new(); + + let nb_int = loop { + println!("Please enter an integer > 0 and <= 1000 :"); + number_str.clear(); + io::stdin() + .read_line(&mut number_str) + .expect("Cannot read your entry."); + let number: i32 = match number_str.trim().parse() { + Ok(n) => { + if n > 1000 || n <= 0 { + continue; + } + n + } + Err(_) => continue, + }; + break number; + }; + + // Recherche et affichage de l'indice dans l'array du nombre correspondant a l'entree utilisateur + + // let now = Instant::now(); + + // Methode par defaut + // + // let result: i32 = match array.iter().position(|&x| x == nb_int) { + // Some(nb) => nb as i32, + // None => -1, + // }; + // + // Temps moyen de recherche : 5 µs + + // Methode dichotomique + // + let result: i32 = search(nb_int, array); + // + // Temps moyen de recherche : 600 ns + + // let new_now = Instant::now(); + // println!("Temps de recherche: {:?}", new_now.duration_since(now)); + + if result != -1 { + println!("L'element est a la position : {}.", result); + } else { + println!("L'element est introuvable."); + } +} + +// FN Recherche dichotomique prenant en parametre l'entier a trouver, sa liste et retourne sa position. +fn search(nb: i32, array: [i32; 255]) -> i32 { + let mut start: i16 = 0; + let mut end: i16 = 254; + let mut mid: i16 = 0; + let mut found: bool = false; + + while !found && start <= end { + mid = (start + end) / 2; + if array[mid as usize] == nb { + found = true; + } else { + if nb > array[mid as usize] { + start = mid + 1; + } else { + end = mid - 1; + } + } + } + if found { + return mid as i32; + } else { + return -1; + } +}