add zig codes for Section 'Stack', 'Bubble Sort' and 'Insertion Sort'

This commit is contained in:
sjinzh
2023-01-12 21:51:11 +08:00
parent 74f65a6ba2
commit 8726934f94
6 changed files with 426 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
// File: time_complexity.zig
// Created Time: 2023-01-08
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// 冒泡排序
fn bubbleSort(nums: []i32) void {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
var i: usize = nums.len - 1;
while (i > 0) : (i -= 1) {
var j: usize = 0;
// 内循环:冒泡操作
while (j < i) : (j += 1) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
var tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
}
}
}
}
// 冒泡排序(标志优化)
fn bubbleSortWithFlag(nums: []i32) void {
// 外循环:待排序元素数量为 n-1, n-2, ..., 1
var i: usize = nums.len - 1;
while (i > 0) : (i -= 1) {
var flag = false; // 初始化标志位
var j: usize = 0;
// 内循环:冒泡操作
while (j < i) : (j += 1) {
if (nums[j] > nums[j + 1]) {
// 交换 nums[j] 与 nums[j + 1]
var tmp = nums[j];
nums[j] = nums[j + 1];
nums[j + 1] = tmp;
flag = true;
}
}
if (!flag) break; // 此轮冒泡未交换任何元素,直接跳出
}
}
// Driver Code
pub fn main() !void {
var nums = [_]i32{ 4, 1, 3, 1, 5, 2 };
bubbleSort(&nums);
std.debug.print("冒泡排序完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums);
var nums1 = [_]i32{ 4, 1, 3, 1, 5, 2 };
bubbleSortWithFlag(&nums1);
std.debug.print("\n冒泡排序完成后 nums1 = ", .{});
inc.PrintUtil.printArray(i32, &nums1);
const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
}

View File

@@ -0,0 +1,33 @@
// File: time_complexity.zig
// Created Time: 2023-01-08
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// 插入排序
fn insertionSort(nums: []i32) void {
// 外循环base = nums[1], nums[2], ..., nums[n-1]
var i: usize = 1;
while (i < nums.len) : (i += 1) {
var base = nums[i];
var j: usize = i;
// 内循环:将 base 插入到左边的正确位置
while (j >= 1 and nums[j - 1] > base) : (j -= 1) {
nums[j] = nums[j - 1]; // 1. 将 nums[j] 向右移动一位
}
nums[j] = base; // 2. 将 base 赋值到正确位置
}
}
// Driver Code
pub fn main() !void {
var nums = [_]i32{ 4, 1, 3, 1, 5, 2 };
insertionSort(&nums);
std.debug.print("插入排序完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums);
const getchar = try std.io.getStdIn().reader().readByte();
_ = getchar;
}