Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- >use rand::thread_rng;
- use std::time::Instant;
- use floating_duration::TimeFormat;
- fn binary_searcher(search_key: i32, vec: &mut Vec<i32>) -> bool {
- let mut low: usize = 0;
- let mut high: usize = vec.len()-1;
- let mut _mid: usize = 0;
- while low <= high {
- _mid = low + (high-low)/2;
- if search_key == vec[_mid] {
- return true;
- }
- if search_key < vec[_mid] {
- high = _mid - 1;
- } else if search_key > vec[_mid] {
- low = _mid + 1;
- }
- }
- return false;
- }
- fn main() {
- let mut _rng = thread_rng();
- let mut int_vec = Vec::new();
- let max_num = 1000000;
- for num in 1..max_num {
- int_vec.push(num as i32);
- }
- let start = Instant::now();
- let _result = binary_searcher(384723, &mut int_vec);
- println!("It took: {} to search", TimeFormat(start.elapsed()));
- }
Advertisement
Add Comment
Please, Sign In to add comment