Jenderal92

---

Feb 3rd, 2021
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. >use rand::thread_rng;
  2. use std::time::Instant;
  3. use floating_duration::TimeFormat;
  4.  
  5. fn binary_searcher(search_key: i32, vec: &mut Vec<i32>) -> bool {
  6. let mut low: usize = 0;
  7. let mut high: usize = vec.len()-1;
  8. let mut _mid: usize = 0;
  9. while low <= high {
  10. _mid = low + (high-low)/2;
  11. if search_key == vec[_mid] {
  12. return true;
  13. }
  14. if search_key < vec[_mid] {
  15. high = _mid - 1;
  16. } else if search_key > vec[_mid] {
  17. low = _mid + 1;
  18. }
  19. }
  20. return false;
  21. }
  22.  
  23. fn main() {
  24. let mut _rng = thread_rng();
  25. let mut int_vec = Vec::new();
  26. let max_num = 1000000;
  27.  
  28. for num in 1..max_num {
  29. int_vec.push(num as i32);
  30. }
  31. let start = Instant::now();
  32. let _result = binary_searcher(384723, &mut int_vec);
  33. println!("It took: {} to search", TimeFormat(start.elapsed()));
  34. }
  35.  
Advertisement
Add Comment
Please, Sign In to add comment