add zig codes for Section Quick Sort, Merge Sort, Radix Sort (#282)

* add zig codes for Section 'Quick Sort' (quick_sort.zig), 'Merge Sort' (merge_sort.zig)

* add zig codes for Section 'Quick Sort' (quick_sort.zig), 'Merge Sort' (merge_sort.zig)

* add zig codes for Section 'Quick Sort' (quick_sort.zig), 'Merge Sort' (merge_sort.zig), 'Radix Sort' (radix_sort.zig)

* add zig codes for Section 'Quick Sort' (quick_sort.zig), 'Merge Sort' (merge_sort.zig), 'Radix Sort' (radix_sort.zig)
This commit is contained in:
sjinzh 2023-01-19 23:27:14 +08:00 committed by GitHub
parent caa775c53d
commit 1cc743e97a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 347 additions and 0 deletions

View file

@ -297,4 +297,46 @@ pub fn build(b: *std.build.Builder) void {
if (b.args) |args| run_cmd_insertion_sort.addArgs(args);
const run_step_insertion_sort = b.step("run_insertion_sort", "Run insertion_sort");
run_step_insertion_sort.dependOn(&run_cmd_insertion_sort.step);
// Section: "Quick Sort"
// Source File: "chapter_sorting/quick_sort.zig"
// Run Command: zig build run_quick_sort
const exe_quick_sort = b.addExecutable("quick_sort", "chapter_sorting/quick_sort.zig");
exe_quick_sort.addPackagePath("include", "include/include.zig");
exe_quick_sort.setTarget(target);
exe_quick_sort.setBuildMode(mode);
exe_quick_sort.install();
const run_cmd_quick_sort = exe_quick_sort.run();
run_cmd_quick_sort.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_quick_sort.addArgs(args);
const run_step_quick_sort = b.step("run_quick_sort", "Run quick_sort");
run_step_quick_sort.dependOn(&run_cmd_quick_sort.step);
// Section: "Merge Sort"
// Source File: "chapter_sorting/merge_sort.zig"
// Run Command: zig build run_merge_sort
const exe_merge_sort = b.addExecutable("merge_sort", "chapter_sorting/merge_sort.zig");
exe_merge_sort.addPackagePath("include", "include/include.zig");
exe_merge_sort.setTarget(target);
exe_merge_sort.setBuildMode(mode);
exe_merge_sort.install();
const run_cmd_merge_sort = exe_merge_sort.run();
run_cmd_merge_sort.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_merge_sort.addArgs(args);
const run_step_merge_sort = b.step("run_merge_sort", "Run merge_sort");
run_step_merge_sort.dependOn(&run_cmd_merge_sort.step);
// Section: "Radix Sort"
// Source File: "chapter_sorting/radix_sort.zig"
// Run Command: zig build run_radix_sort
const exe_radix_sort = b.addExecutable("radix_sort", "chapter_sorting/radix_sort.zig");
exe_radix_sort.addPackagePath("include", "include/include.zig");
exe_radix_sort.setTarget(target);
exe_radix_sort.setBuildMode(mode);
exe_radix_sort.install();
const run_cmd_radix_sort = exe_radix_sort.run();
run_cmd_radix_sort.step.dependOn(b.getInstallStep());
if (b.args) |args| run_cmd_radix_sort.addArgs(args);
const run_step_radix_sort = b.step("run_radix_sort", "Run radix_sort");
run_step_radix_sort.dependOn(&run_cmd_radix_sort.step);
}

View file

@ -0,0 +1,67 @@
// File: merge_sort.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
//
// [left, mid]
// [mid + 1, right]
fn merge(nums: []i32, left: usize, mid: usize, right: usize) !void {
//
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
defer mem_arena.deinit();
const mem_allocator = mem_arena.allocator();
var tmp = try mem_allocator.alloc(i32, right + 1 - left);
std.mem.copy(i32, tmp, nums[left..right+1]);
//
var leftStart = left - left;
var leftEnd = mid - left;
//
var rightStart = mid + 1 - left;
var rightEnd = right - left;
// i, j
var i = leftStart;
var j = rightStart;
// nums
var k = left;
while (k <= right) : (k += 1) {
// j++
if (i > leftEnd) {
nums[k] = tmp[j];
j += 1;
// <= i++
} else if (j > rightEnd or tmp[i] <= tmp[j]) {
nums[k] = tmp[i];
i += 1;
// > j++
} else {
nums[k] = tmp[j];
j += 1;
}
}
}
//
fn mergeSort(nums: []i32, left: usize, right: usize) !void {
//
if (left >= right) return; // 1
//
var mid = (left + right) / 2; //
try mergeSort(nums, left, mid); //
try mergeSort(nums, mid + 1, right); //
//
try merge(nums, left, mid, right);
}
// Driver Code
pub fn main() !void {
//
var nums = [_]i32{ 7, 3, 2, 6, 0, 1, 5, 4 };
try mergeSort(&nums, 0, nums.len - 1);
std.debug.print("归并排序完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums);
_ = try std.io.getStdIn().reader().readByte();
}

View file

@ -0,0 +1,160 @@
// File: quick_sort.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
//
const QuickSort = struct {
//
pub fn swap(nums: []i32, i: usize, j: usize) void {
var tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
//
pub fn partition(nums: []i32, left: usize, right: usize) usize {
// nums[left]
var i = left;
var j = right;
while (i < j) {
while (i < j and nums[j] >= nums[left]) j -= 1; //
while (i < j and nums[i] <= nums[left]) i += 1; //
swap(nums, i, j); //
}
swap(nums, i, left); // 线
return i; //
}
//
pub fn quickSort(nums: []i32, left: usize, right: usize) void {
// 1
if (left >= right) return;
//
var pivot = partition(nums, left, right);
//
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
};
//
const QuickSortMedian = struct {
//
pub fn swap(nums: []i32, i: usize, j: usize) void {
var tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
//
pub fn medianThree(nums: []i32, left: usize, mid: usize, right: usize) usize {
// 使
// 0 ^ 0 = 1 ^ 1 = 0, 0 ^ 1 = 1 ^ 0 = 1
if ((nums[left] < nums[mid]) != (nums[left] < nums[right])) {
return left;
} else if ((nums[mid] < nums[left]) != (nums[mid] < nums[right])) {
return mid;
} else {
return right;
}
}
//
pub fn partition(nums: []i32, left: usize, right: usize) usize {
//
var med = medianThree(nums, left, (left + right) / 2, right);
//
swap(nums, left, med);
// nums[left]
var i = left;
var j = right;
while (i < j) {
while (i < j and nums[j] >= nums[left]) j -= 1; //
while (i < j and nums[i] <= nums[left]) i += 1; //
swap(nums, i, j); //
}
swap(nums, i, left); // 线
return i; //
}
//
pub fn quickSort(nums: []i32, left: usize, right: usize) void {
// 1
if (left >= right) return;
//
var pivot = partition(nums, left, right);
if (pivot == 0) return;
//
quickSort(nums, left, pivot - 1);
quickSort(nums, pivot + 1, right);
}
};
//
const QuickSortTailCall = struct {
//
pub fn swap(nums: []i32, i: usize, j: usize) void {
var tmp = nums[i];
nums[i] = nums[j];
nums[j] = tmp;
}
//
pub fn partition(nums: []i32, left: usize, right: usize) usize {
// nums[left]
var i = left;
var j = right;
while (i < j) {
while (i < j and nums[j] >= nums[left]) j -= 1; //
while (i < j and nums[i] <= nums[left]) i += 1; //
swap(nums, i, j); //
}
swap(nums, i, left); // 线
return i; //
}
//
pub fn quickSort(nums: []i32, left_: usize, right_: usize) void {
var left = left_;
var right = right_;
// 1
while (left < right) {
//
var pivot = partition(nums, left, right);
//
if (pivot - left < right - pivot) {
quickSort(nums, left, pivot - 1); //
left = pivot + 1; // [pivot + 1, right]
} else {
quickSort(nums, pivot + 1, right); //
right = pivot - 1; // [left, pivot - 1]
}
}
}
};
// Driver Code
pub fn main() !void {
//
var nums = [_]i32{ 2, 4, 1, 0, 3, 5 };
QuickSort.quickSort(&nums, 0, nums.len - 1);
std.debug.print("快速排序完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums);
//
var nums1 = [_]i32{ 2, 4, 1, 0, 3, 5 };
QuickSortMedian.quickSort(&nums1, 0, nums1.len - 1);
std.debug.print("\n快速排序(中位基准数优化)完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums1);
//
var nums2 = [_]i32{ 2, 4, 1, 0, 3, 5 };
QuickSortTailCall.quickSort(&nums2, 0, nums2.len - 1);
std.debug.print("\n快速排序(尾递归优化)完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums2);
_ = try std.io.getStdIn().reader().readByte();
}

View file

@ -0,0 +1,78 @@
// File: radix_sort.zig
// Created Time: 2023-01-15
// Author: sjinzh (sjinzh@gmail.com)
const std = @import("std");
const inc = @import("include");
// num k exp = 10^(k-1)
fn digit(num: i32, exp: i32) i32 {
// exp k
return @mod(@divFloor(num, exp), 10);
}
// nums k
fn countSort(nums: []i32, exp: i32) !void {
// 0~9 10
var mem_arena = std.heap.ArenaAllocator.init(std.heap.page_allocator);
// defer mem_arena.deinit();
const mem_allocator = mem_arena.allocator();
var bucket = try mem_allocator.alloc(usize, 10);
std.mem.set(usize, bucket, 0);
var n = nums.len;
// 0~9
for (nums) |num| {
var d = @bitCast(u32, digit(num, exp)); // nums[i] k d
bucket[d] += 1; // d
}
//
var i: usize = 1;
while (i < 10) : (i += 1) {
bucket[i] += bucket[i - 1];
}
// tmp
var tmp = try mem_allocator.alloc(i32, n);
i = n - 1;
while (i >= 0) : (i -= 1) {
var d = @bitCast(u32, digit(nums[i], exp));
var j = bucket[d] - 1; // d j
tmp[j] = nums[i]; // j
bucket[d] -= 1; // d 1
if (i == 0) break;
}
// tmp nums
i = 0;
while (i < n) : (i += 1) {
nums[i] = tmp[i];
}
}
//
fn radixSort(nums: []i32) !void {
//
var ma: i32 = std.math.minInt(i32);
for (nums) |num| {
if (num > ma) ma = num;
}
//
var exp: i32 = 1;
while (ma >= exp) : (exp *= 10) {
// k
// k = 1 -> exp = 1
// k = 2 -> exp = 10
// k = 3 -> exp = 100
// exp = 10^(k-1)
try countSort(nums, exp);
}
}
// Driver Code
pub fn main() !void {
//
var nums = [_]i32{ 23, 12, 3, 4, 788, 192 };
try radixSort(&nums);
std.debug.print("基数排序完成后 nums = ", .{});
inc.PrintUtil.printArray(i32, &nums);
_ = try std.io.getStdIn().reader().readByte();
}