mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-13 09:49:49 +08:00
zig : upgrade codes && rust : add codes for chapter_searching and chapter_dynamic_programming. (#591)
* zig : update zig codes * rust : add codes for linear_search and hashing_search * rust : add codes for linear_search and hashing_search * rust : add codes for chapter_dynamic_programming
This commit is contained in:
@@ -54,7 +54,7 @@ fn quadratic(n: i32) i32 {
|
||||
fn bubbleSort(nums: []i32) i32 {
|
||||
var count: i32 = 0; // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
var i: i32 = @intCast(i32, nums.len ) - 1;
|
||||
var i: i32 = @as(i32, @intCast(nums.len)) - 1;
|
||||
while (i > 0) : (i -= 1) {
|
||||
var j: usize = 0;
|
||||
// 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端
|
||||
@@ -154,7 +154,7 @@ pub fn main() !void {
|
||||
count = quadratic(n);
|
||||
std.debug.print("平方阶的计算操作数量 = {}\n", .{count});
|
||||
for (&nums, 0..) |*num, i| {
|
||||
num.* = n - @intCast(i32, i); // [n,n-1,...,2,1]
|
||||
num.* = n - @as(i32, @intCast(i)); // [n,n-1,...,2,1]
|
||||
}
|
||||
count = bubbleSort(&nums);
|
||||
std.debug.print("平方阶(冒泡排序)的计算操作数量 = {}\n", .{count});
|
||||
|
||||
@@ -10,7 +10,7 @@ pub fn randomNumbers(comptime n: usize) [n]i32 {
|
||||
var nums: [n]i32 = undefined;
|
||||
// 生成数组 nums = { 1, 2, 3, ..., n }
|
||||
for (&nums, 0..) |*num, i| {
|
||||
num.* = @intCast(i32, i) + 1;
|
||||
num.* = @as(i32, @intCast(i)) + 1;
|
||||
}
|
||||
// 随机打乱数组元素
|
||||
const rand = std.crypto.random;
|
||||
@@ -23,7 +23,7 @@ pub fn findOne(nums: []i32) i32 {
|
||||
for (nums, 0..) |num, i| {
|
||||
// 当元素 1 在数组头部时,达到最佳时间复杂度 O(1)
|
||||
// 当元素 1 在数组尾部时,达到最差时间复杂度 O(n)
|
||||
if (num == 1) return @intCast(i32, i);
|
||||
if (num == 1) return @intCast(i);
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user