mirror of
https://github.com/krahets/hello-algo.git
synced 2026-04-14 02:10:37 +08:00
code: update zig 0.14.1 for the chapter of array_and_linkedlist and computational_complexity (#1787)
* update zig array list chapter * update not need change codes. * fix some pr issues and update time space chapter
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// File: iteration.zig
|
||||
// Created Time: 2023-09-27
|
||||
// Author: QiLOL (pikaqqpika@gmail.com)
|
||||
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
const Allocator = std.mem.Allocator;
|
||||
@@ -9,20 +9,19 @@ const Allocator = std.mem.Allocator;
|
||||
fn forLoop(n: usize) i32 {
|
||||
var res: i32 = 0;
|
||||
// 循环求和 1, 2, ..., n-1, n
|
||||
for (1..n+1) |i| {
|
||||
res = res + @as(i32, @intCast(i));
|
||||
for (1..n + 1) |i| {
|
||||
res += @intCast(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
// while 循环
|
||||
fn whileLoop(n: i32) i32 {
|
||||
var res: i32 = 0;
|
||||
var i: i32 = 1; // 初始化条件变量
|
||||
// 循环求和 1, 2, ..., n-1, n
|
||||
while (i <= n) {
|
||||
while (i <= n) : (i += 1) {
|
||||
res += @intCast(i);
|
||||
i += 1;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -32,11 +31,12 @@ fn whileLoopII(n: i32) i32 {
|
||||
var res: i32 = 0;
|
||||
var i: i32 = 1; // 初始化条件变量
|
||||
// 循环求和 1, 4, 10, ...
|
||||
while (i <= n) {
|
||||
res += @intCast(i);
|
||||
while (i <= n) : ({
|
||||
// 更新条件变量
|
||||
i += 1;
|
||||
i *= 2;
|
||||
}) {
|
||||
res += @intCast(i);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@@ -47,31 +47,45 @@ fn nestedForLoop(allocator: Allocator, n: usize) ![]const u8 {
|
||||
defer res.deinit();
|
||||
var buffer: [20]u8 = undefined;
|
||||
// 循环 i = 1, 2, ..., n-1, n
|
||||
for (1..n+1) |i| {
|
||||
for (1..n + 1) |i| {
|
||||
// 循环 j = 1, 2, ..., n-1, n
|
||||
for (1..n+1) |j| {
|
||||
var _str = try std.fmt.bufPrint(&buffer, "({d}, {d}), ", .{i, j});
|
||||
try res.appendSlice(_str);
|
||||
for (1..n + 1) |j| {
|
||||
const str = try std.fmt.bufPrint(&buffer, "({d}, {d}), ", .{ i, j });
|
||||
try res.appendSlice(str);
|
||||
}
|
||||
}
|
||||
return res.toOwnedSlice();
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() !void {
|
||||
var gpa = std.heap.DebugAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const n: i32 = 5;
|
||||
var res: i32 = 0;
|
||||
|
||||
res = forLoop(n);
|
||||
std.debug.print("\nfor 循环的求和结果 res = {}\n", .{res});
|
||||
std.debug.print("for 循环的求和结果 res = {}\n", .{res});
|
||||
|
||||
res = whileLoop(n);
|
||||
std.debug.print("\nwhile 循环的求和结果 res = {}\n", .{res});
|
||||
std.debug.print("while 循环的求和结果 res = {}\n", .{res});
|
||||
|
||||
res = whileLoopII(n);
|
||||
std.debug.print("\nwhile 循环(两次更新)求和结果 res = {}\n", .{res});
|
||||
std.debug.print("while 循环(两次更新)求和结果 res = {}\n", .{res});
|
||||
|
||||
const allocator = std.heap.page_allocator;
|
||||
const resStr = try nestedForLoop(allocator, n);
|
||||
std.debug.print("\n双层 for 循环的遍历结果 {s}\n", .{resStr});
|
||||
std.debug.print("双层 for 循环的遍历结果 {s}\n", .{resStr});
|
||||
allocator.free(resStr);
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
try run();
|
||||
}
|
||||
|
||||
test "interation" {
|
||||
try run();
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// File: recursion.zig
|
||||
// Created Time: 2023-09-27
|
||||
// Author: QiLOL (pikaqqpika@gmail.com)
|
||||
|
||||
// Author: QiLOL (pikaqqpika@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
// 递归函数
|
||||
@@ -11,7 +11,7 @@ fn recur(n: i32) i32 {
|
||||
return 1;
|
||||
}
|
||||
// 递:递归调用
|
||||
var res: i32 = recur(n - 1);
|
||||
const res = recur(n - 1);
|
||||
// 归:返回结果
|
||||
return n + res;
|
||||
}
|
||||
@@ -54,25 +54,35 @@ fn fib(n: i32) i32 {
|
||||
return n - 1;
|
||||
}
|
||||
// 递归调用 f(n) = f(n-1) + f(n-2)
|
||||
var res: i32 = fib(n - 1) + fib(n - 2);
|
||||
const res: i32 = fib(n - 1) + fib(n - 2);
|
||||
// 返回结果 f(n)
|
||||
return res;
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() void {
|
||||
const n: i32 = 5;
|
||||
var res: i32 = 0;
|
||||
|
||||
res = recur(n);
|
||||
std.debug.print("\n递归函数的求和结果 res = {}\n", .{recur(n)});
|
||||
std.debug.print("递归函数的求和结果 res = {}\n", .{recur(n)});
|
||||
|
||||
res = forLoopRecur(n);
|
||||
std.debug.print("\n使用迭代模拟递归的求和结果 res = {}\n", .{forLoopRecur(n)});
|
||||
std.debug.print("使用迭代模拟递归的求和结果 res = {}\n", .{forLoopRecur(n)});
|
||||
|
||||
res = tailRecur(n, 0);
|
||||
std.debug.print("\n尾递归函数的求和结果 res = {}\n", .{tailRecur(n, 0)});
|
||||
std.debug.print("尾递归函数的求和结果 res = {}\n", .{tailRecur(n, 0)});
|
||||
|
||||
res = fib(n);
|
||||
std.debug.print("\n斐波那契数列的第 {} 项为 {}\n", .{n, fib(n)});
|
||||
std.debug.print("斐波那契数列的第 {} 项为 {}\n", .{ n, fib(n) });
|
||||
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() void {
|
||||
run();
|
||||
}
|
||||
|
||||
test "recursion" {
|
||||
run();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
// File: space_complexity.zig
|
||||
// Created Time: 2023-01-07
|
||||
// Author: codingonion (coderonion@gmail.com)
|
||||
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
const utils = @import("utils");
|
||||
const ListNode = utils.ListNode;
|
||||
const TreeNode = utils.TreeNode;
|
||||
|
||||
// 函数
|
||||
fn function() i32 {
|
||||
@@ -15,13 +17,13 @@ fn function() i32 {
|
||||
fn constant(n: i32) void {
|
||||
// 常量、变量、对象占用 O(1) 空间
|
||||
const a: i32 = 0;
|
||||
var b: i32 = 0;
|
||||
var nums = [_]i32{0}**10000;
|
||||
var node = inc.ListNode(i32){.val = 0};
|
||||
const b: i32 = 0;
|
||||
const nums = [_]i32{0} ** 10000;
|
||||
const node = ListNode(i32){ .val = 0 };
|
||||
var i: i32 = 0;
|
||||
// 循环中的变量占用 O(1) 空间
|
||||
while (i < n) : (i += 1) {
|
||||
var c: i32 = 0;
|
||||
const c: i32 = 0;
|
||||
_ = c;
|
||||
}
|
||||
// 循环中的函数占用 O(1) 空间
|
||||
@@ -38,7 +40,7 @@ fn constant(n: i32) void {
|
||||
// 线性阶
|
||||
fn linear(comptime n: i32) !void {
|
||||
// 长度为 n 的数组占用 O(n) 空间
|
||||
var nums = [_]i32{0}**n;
|
||||
const nums = [_]i32{0} ** n;
|
||||
// 长度为 n 的列表占用 O(n) 空间
|
||||
var nodes = std.ArrayList(i32).init(std.heap.page_allocator);
|
||||
defer nodes.deinit();
|
||||
@@ -85,23 +87,35 @@ fn quadratic(n: i32) !void {
|
||||
// 平方阶(递归实现)
|
||||
fn quadraticRecur(comptime n: i32) i32 {
|
||||
if (n <= 0) return 0;
|
||||
var nums = [_]i32{0}**n;
|
||||
std.debug.print("递归 n = {} 中的 nums 长度 = {}\n", .{n, nums.len});
|
||||
const nums = [_]i32{0} ** n;
|
||||
std.debug.print("递归 n = {} 中的 nums 长度 = {}\n", .{ n, nums.len });
|
||||
return quadraticRecur(n - 1);
|
||||
}
|
||||
|
||||
// 指数阶(建立满二叉树)
|
||||
fn buildTree(mem_allocator: std.mem.Allocator, n: i32) !?*inc.TreeNode(i32) {
|
||||
fn buildTree(allocator: std.mem.Allocator, n: i32) !?*TreeNode(i32) {
|
||||
if (n == 0) return null;
|
||||
const root = try mem_allocator.create(inc.TreeNode(i32));
|
||||
const root = try allocator.create(TreeNode(i32));
|
||||
root.init(0);
|
||||
root.left = try buildTree(mem_allocator, n - 1);
|
||||
root.right = try buildTree(mem_allocator, n - 1);
|
||||
root.left = try buildTree(allocator, n - 1);
|
||||
root.right = try buildTree(allocator, n - 1);
|
||||
return root;
|
||||
}
|
||||
|
||||
// 释放树的内存
|
||||
fn freeTree(allocator: std.mem.Allocator, root: ?*const TreeNode(i32)) void {
|
||||
if (root == null) return;
|
||||
freeTree(allocator, root.?.left);
|
||||
freeTree(allocator, root.?.right);
|
||||
allocator.destroy(root.?);
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() !void {
|
||||
var gpa = std.heap.DebugAllocator(.{}){};
|
||||
defer _ = gpa.deinit();
|
||||
const allocator = gpa.allocator();
|
||||
|
||||
const n: i32 = 5;
|
||||
// 常数阶
|
||||
constant(n);
|
||||
@@ -112,13 +126,17 @@ pub fn main() !void {
|
||||
try quadratic(n);
|
||||
_ = quadraticRecur(n);
|
||||
// 指数阶
|
||||
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
|
||||
defer mem_arena.deinit();
|
||||
var root = blk_root: {
|
||||
const mem_allocator = mem_arena.allocator();
|
||||
break :blk_root try buildTree(mem_allocator, n);
|
||||
};
|
||||
try inc.PrintUtil.printTree(root, null, false);
|
||||
const root = try buildTree(allocator, n);
|
||||
defer freeTree(allocator, root);
|
||||
std.debug.print("{}\n", .{utils.fmt.tree(i32, root)});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
}
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
try run();
|
||||
}
|
||||
|
||||
test "space_complexity" {
|
||||
try run();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// File: time_complexity.zig
|
||||
// Created Time: 2022-12-28
|
||||
// Author: codingonion (coderonion@gmail.com)
|
||||
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
|
||||
@@ -10,7 +10,7 @@ fn constant(n: i32) i32 {
|
||||
var count: i32 = 0;
|
||||
const size: i32 = 100_000;
|
||||
var i: i32 = 0;
|
||||
while(i<size) : (i += 1) {
|
||||
while (i < size) : (i += 1) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
@@ -52,7 +52,7 @@ fn quadratic(n: i32) i32 {
|
||||
|
||||
// 平方阶(冒泡排序)
|
||||
fn bubbleSort(nums: []i32) i32 {
|
||||
var count: i32 = 0; // 计数器
|
||||
var count: i32 = 0; // 计数器
|
||||
// 外循环:未排序区间为 [0, i]
|
||||
var i: i32 = @as(i32, @intCast(nums.len)) - 1;
|
||||
while (i > 0) : (i -= 1) {
|
||||
@@ -61,10 +61,10 @@ fn bubbleSort(nums: []i32) i32 {
|
||||
while (j < i) : (j += 1) {
|
||||
if (nums[j] > nums[j + 1]) {
|
||||
// 交换 nums[j] 与 nums[j + 1]
|
||||
var tmp = nums[j];
|
||||
const tmp = nums[j];
|
||||
nums[j] = nums[j + 1];
|
||||
nums[j + 1] = tmp;
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
count += 3; // 元素交换包含 3 个单元操作
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -97,11 +97,9 @@ fn expRecur(n: i32) i32 {
|
||||
// 对数阶(循环实现)
|
||||
fn logarithmic(n: i32) i32 {
|
||||
var count: i32 = 0;
|
||||
var n_var = n;
|
||||
while (n_var > 1)
|
||||
{
|
||||
n_var = n_var / 2;
|
||||
count +=1;
|
||||
var n_var: i32 = n;
|
||||
while (n_var > 1) : (n_var = @divTrunc(n_var, 2)) {
|
||||
count += 1;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
@@ -109,13 +107,13 @@ fn logarithmic(n: i32) i32 {
|
||||
// 对数阶(递归实现)
|
||||
fn logRecur(n: i32) i32 {
|
||||
if (n <= 1) return 0;
|
||||
return logRecur(n / 2) + 1;
|
||||
return logRecur(@divTrunc(n, 2)) + 1;
|
||||
}
|
||||
|
||||
// 线性对数阶
|
||||
fn linearLogRecur(n: i32) i32 {
|
||||
if (n <= 1) return 1;
|
||||
var count: i32 = linearLogRecur(n / 2) + linearLogRecur(n / 2);
|
||||
var count: i32 = linearLogRecur(@divTrunc(n, 2)) + linearLogRecur(@divTrunc(n, 2));
|
||||
var i: i32 = 0;
|
||||
while (i < n) : (i += 1) {
|
||||
count += 1;
|
||||
@@ -136,7 +134,7 @@ fn factorialRecur(n: i32) i32 {
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() void {
|
||||
// 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势
|
||||
const n: i32 = 8;
|
||||
std.debug.print("输入数据大小 n = {}\n", .{n});
|
||||
@@ -146,14 +144,14 @@ pub fn main() !void {
|
||||
|
||||
count = linear(n);
|
||||
std.debug.print("线性阶的操作数量 = {}\n", .{count});
|
||||
var nums = [_]i32{0}**n;
|
||||
var nums = [_]i32{0} ** n;
|
||||
count = arrayTraversal(&nums);
|
||||
std.debug.print("线性阶(遍历数组)的操作数量 = {}\n", .{count});
|
||||
|
||||
count = quadratic(n);
|
||||
std.debug.print("平方阶的操作数量 = {}\n", .{count});
|
||||
for (&nums, 0..) |*num, i| {
|
||||
num.* = n - @as(i32, @intCast(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});
|
||||
@@ -174,6 +172,13 @@ pub fn main() !void {
|
||||
count = factorialRecur(n);
|
||||
std.debug.print("阶乘阶(递归实现)的操作数量 = {}\n", .{count});
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
run();
|
||||
}
|
||||
|
||||
test "time_complexity" {
|
||||
run();
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
// File: worst_best_time_complexity.zig
|
||||
// Created Time: 2022-12-28
|
||||
// Author: codingonion (coderonion@gmail.com)
|
||||
// Author: codingonion (coderonion@gmail.com), CreatorMetaSky (creator_meta_sky@163.com)
|
||||
|
||||
const std = @import("std");
|
||||
const inc = @import("include");
|
||||
const utils = @import("utils");
|
||||
|
||||
// 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱
|
||||
pub fn randomNumbers(comptime n: usize) [n]i32 {
|
||||
@@ -29,17 +29,25 @@ pub fn findOne(nums: []i32) i32 {
|
||||
}
|
||||
|
||||
// Driver Code
|
||||
pub fn main() !void {
|
||||
pub fn run() void {
|
||||
var i: i32 = 0;
|
||||
while (i < 10) : (i += 1) {
|
||||
const n: usize = 100;
|
||||
var nums = randomNumbers(n);
|
||||
var index = findOne(&nums);
|
||||
std.debug.print("\n数组 [ 1, 2, ..., n ] 被打乱后 = ", .{});
|
||||
inc.PrintUtil.printArray(i32, &nums);
|
||||
const index = findOne(&nums);
|
||||
std.debug.print("数组 [ 1, 2, ..., n ] 被打乱后 = ", .{});
|
||||
std.debug.print("{}\n", .{utils.fmt.slice(nums)});
|
||||
|
||||
std.debug.print("数字 1 的索引为 {}\n", .{index});
|
||||
}
|
||||
|
||||
_ = try std.io.getStdIn().reader().readByte();
|
||||
std.debug.print("\n", .{});
|
||||
}
|
||||
|
||||
pub fn main() !void {
|
||||
run();
|
||||
}
|
||||
|
||||
test "worst_best_time_complexity" {
|
||||
run();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user