mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 13:40:36 +08:00
update rust codes for hash_map, binary_search, bubble_sort, stack, queue (#330)
* update rust codes * update rust codes * update rust codes * update and add rust codes for hash_map, binary_search, bubble_sort * update and add rust codes for hash_map, binary_search, bubble_sort * add rust codes for chapter stack * add rust codes for chapter queue * add rust codes for chapter deque
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
/**
|
||||
* File: time_complexity.rs
|
||||
* Created Time: 2023-01-10
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com)
|
||||
* Author: xBLACICEx (xBLACKICEx@outlook.com), sjinzh (sjinzh@gmail.com)
|
||||
*/
|
||||
#[allow(unused_variables)]
|
||||
|
||||
/* 常数阶 */
|
||||
fn constant(n: i32) -> i32 {
|
||||
_ = n;
|
||||
let mut count = 0;
|
||||
let size = 100000;
|
||||
let size = 100_000;
|
||||
for _ in 0..size {
|
||||
count += 1
|
||||
count += 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
@@ -34,6 +34,7 @@ fn array_traversal(nums: &[i32]) -> i32 {
|
||||
count
|
||||
}
|
||||
|
||||
/* 平方阶 */
|
||||
fn quadratic(n: i32) -> i32 {
|
||||
let mut count = 0;
|
||||
// 循环次数与数组长度成平方关系
|
||||
@@ -88,30 +89,30 @@ fn exp_recur(n: i32) -> i32 {
|
||||
}
|
||||
|
||||
/* 对数阶(循环实现) */
|
||||
fn logarithmic(mut n: i32) -> i32 {
|
||||
fn logarithmic(mut n: f32) -> i32 {
|
||||
let mut count = 0;
|
||||
|
||||
while n > 1 {
|
||||
n = n / 2;
|
||||
while n > 1.0 {
|
||||
n = n / 2.0;
|
||||
count += 1;
|
||||
}
|
||||
count
|
||||
}
|
||||
|
||||
/* 对数阶(递归实现) */
|
||||
fn log_recur(n: i32) -> i32 {
|
||||
if n <= 1 {
|
||||
fn log_recur(n: f32) -> i32 {
|
||||
if n <= 1.0 {
|
||||
return 0;
|
||||
}
|
||||
log_recur(n / 2) + 1
|
||||
log_recur(n / 2.0) + 1
|
||||
}
|
||||
|
||||
/* 线性对数阶 */
|
||||
fn linear_log_recur(n: f64) -> i32 {
|
||||
fn linear_log_recur(n: f32) -> i32 {
|
||||
if n <= 1.0 {
|
||||
return 1;
|
||||
}
|
||||
let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0);
|
||||
let mut count = linear_log_recur(n / 2.0) +
|
||||
linear_log_recur(n / 2.0);
|
||||
for _ in 0 ..n as i32 {
|
||||
count += 1;
|
||||
}
|
||||
@@ -147,7 +148,6 @@ fn main() {
|
||||
|
||||
count = quadratic(n);
|
||||
println!("平方阶的计算操作数量 = {}", count);
|
||||
|
||||
let mut nums = (1..=n).rev().collect::<Vec<_>>(); // [n,n-1,...,2,1]
|
||||
count = bubble_sort(&mut nums);
|
||||
println!("平方阶(冒泡排序)的计算操作数量 = {}", count);
|
||||
@@ -157,12 +157,12 @@ fn main() {
|
||||
count = exp_recur(n);
|
||||
println!("指数阶(递归实现)的计算操作数量 = {}", count);
|
||||
|
||||
count = logarithmic(n);
|
||||
count = logarithmic(n as f32);
|
||||
println!("对数阶(循环实现)的计算操作数量 = {}", count);
|
||||
count = log_recur(n);
|
||||
count = log_recur(n as f32);
|
||||
println!("对数阶(递归实现)的计算操作数量 = {}", count);
|
||||
|
||||
count = linear_log_recur(n.into());
|
||||
count = linear_log_recur(n as f32);
|
||||
println!("线性对数阶(递归实现)的计算操作数量 = {}", count);
|
||||
|
||||
count = factorial_recur(n);
|
||||
|
||||
Reference in New Issue
Block a user