forked from krahets/hello-algo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ feat(worst_best_time_complexity): add rust code
- Loading branch information
xblakicex
committed
Jan 13, 2023
1 parent
89146b7
commit 705fc86
Showing
1 changed file
with
43 additions
and
0 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
codes/rust/chapter_computational_complexity/worst_best_time_complexity.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// rand = "0.8.5" | ||
/** | ||
* File: time_complexity.cpp | ||
* Created Time: 2023-01-13 | ||
* Author: xBLACICEx ([email protected] ) | ||
*/ | ||
|
||
// to compilse and run this singile file need: | ||
// 1. cargo install cargo-single | ||
// 2. cargo single run worst_best_time_complexity.rs | ||
|
||
use rand::seq::SliceRandom; | ||
use rand::thread_rng; | ||
|
||
/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ | ||
fn random_numbers(n: i32) -> Vec<i32> { | ||
// 生成数组 nums = { 1, 2, 3, ..., n } | ||
let mut nums = (1..n + 1).collect::<Vec<i32>>(); | ||
// 随机打乱数组元素 | ||
nums.shuffle(&mut thread_rng()); | ||
nums | ||
} | ||
|
||
/* 查找数组 nums 中数字 1 所在索引 */ | ||
fn find_one(nums: &[i32]) -> Option<usize> { | ||
for i in 0..nums.len() { | ||
if nums[i] == 1 { | ||
return Some(i); | ||
} | ||
} | ||
None | ||
} | ||
|
||
/* Driver Code */ | ||
fn main() { | ||
for _ in 0..10 { | ||
let n = 100; | ||
let nums = random_numbers(n); | ||
let index = find_one(&nums); | ||
println!("\n数组 [ 1, 2, ..., n ] 被打乱后 = {:?}", nums); | ||
println!("数字 1 的索引为 {:?}", index); | ||
} | ||
} |