about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--.gitignore2
-rw-r--r--README.md4
-rw-r--r--build.zig51
-rw-r--r--build.zig.zon72
-rw-r--r--src/benchmark.zig55
-rw-r--r--src/main.zig3
-rw-r--r--src/root.zig0
-rw-r--r--src/sort/bubble.zig20
-rw-r--r--src/sort/insertion.zig19
-rw-r--r--src/sort/selection.zig22
10 files changed, 248 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..d8c8979
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+.zig-cache
+zig-out
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..fe5b46b
--- /dev/null
+++ b/README.md
@@ -0,0 +1,4 @@
+## DSAZITY - exercising in dsa
+
+Implementing different DSA just for learning.
+This code is not intended to be used anywhere.
diff --git a/build.zig b/build.zig
new file mode 100644
index 0000000..ad5ad10
--- /dev/null
+++ b/build.zig
@@ -0,0 +1,51 @@
+const std = @import("std");
+pub fn build(b: *std.Build) void {
+    const target = b.standardTargetOptions(.{});
+    const optimize = b.standardOptimizeOption(.{});
+
+    const lib = b.addStaticLibrary(.{
+        .name = "dsazitty",
+        .root_source_file = b.path("src/root.zig"),
+        .target = target,
+        .optimize = optimize,
+    });
+
+    b.installArtifact(lib);
+
+    const exe = b.addExecutable(.{
+        .name = "dsazitty",
+        .root_source_file = b.path("src/main.zig"),
+        .target = target,
+        .optimize = optimize,
+    });
+
+    b.installArtifact(exe);
+    const run_cmd = b.addRunArtifact(exe);
+
+    run_cmd.step.dependOn(b.getInstallStep());
+
+    if (b.args) |args| {
+        run_cmd.addArgs(args);
+    }
+
+    const run_step = b.step("run", "Run the app");
+    run_step.dependOn(&run_cmd.step);
+    const lib_unit_tests = b.addTest(.{
+        .root_source_file = b.path("src/root.zig"),
+        .target = target,
+        .optimize = optimize,
+    });
+
+    const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
+
+    const exe_unit_tests = b.addTest(.{
+        .root_source_file = b.path("src/main.zig"),
+        .target = target,
+        .optimize = optimize,
+    });
+
+    const run_exe_unit_tests = b.addRunArtifact(exe_unit_tests);
+    const test_step = b.step("test", "Run unit tests");
+    test_step.dependOn(&run_lib_unit_tests.step);
+    test_step.dependOn(&run_exe_unit_tests.step);
+}
diff --git a/build.zig.zon b/build.zig.zon
new file mode 100644
index 0000000..a7a211c
--- /dev/null
+++ b/build.zig.zon
@@ -0,0 +1,72 @@
+.{
+    // This is the default name used by packages depending on this one. For
+    // example, when a user runs `zig fetch --save <url>`, this field is used
+    // as the key in the `dependencies` table. Although the user can choose a
+    // different name, most users will stick with this provided value.
+    //
+    // It is redundant to include "zig" in this name because it is already
+    // within the Zig package namespace.
+    .name = "dsazitty",
+
+    // This is a [Semantic Version](https://semver.org/).
+    // In a future version of Zig it will be used for package deduplication.
+    .version = "0.0.0",
+
+    // This field is optional.
+    // This is currently advisory only; Zig does not yet do anything
+    // with this value.
+    //.minimum_zig_version = "0.11.0",
+
+    // This field is optional.
+    // Each dependency must either provide a `url` and `hash`, or a `path`.
+    // `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
+    // Once all dependencies are fetched, `zig build` no longer requires
+    // internet connectivity.
+    .dependencies = .{
+        // See `zig fetch --save <url>` for a command-line interface for adding dependencies.
+        //.example = .{
+        //    // When updating this field to a new URL, be sure to delete the corresponding
+        //    // `hash`, otherwise you are communicating that you expect to find the old hash at
+        //    // the new URL.
+        //    .url = "https://example.com/foo.tar.gz",
+        //
+        //    // This is computed from the file contents of the directory of files that is
+        //    // obtained after fetching `url` and applying the inclusion rules given by
+        //    // `paths`.
+        //    //
+        //    // This field is the source of truth; packages do not come from a `url`; they
+        //    // come from a `hash`. `url` is just one of many possible mirrors for how to
+        //    // obtain a package matching this `hash`.
+        //    //
+        //    // Uses the [multihash](https://multiformats.io/multihash/) format.
+        //    .hash = "...",
+        //
+        //    // When this is provided, the package is found in a directory relative to the
+        //    // build root. In this case the package's hash is irrelevant and therefore not
+        //    // computed. This field and `url` are mutually exclusive.
+        //    .path = "foo",
+
+        //    // When this is set to `true`, a package is declared to be lazily
+        //    // fetched. This makes the dependency only get fetched if it is
+        //    // actually used.
+        //    .lazy = false,
+        //},
+    },
+
+    // Specifies the set of files and directories that are included in this package.
+    // Only files and directories listed here are included in the `hash` that
+    // is computed for this package. Only files listed here will remain on disk
+    // when using the zig package manager. As a rule of thumb, one should list
+    // files required for compilation plus any license(s).
+    // Paths are relative to the build root. Use the empty string (`""`) to refer to
+    // the build root itself.
+    // A directory listed here means that all files within, recursively, are included.
+    .paths = .{
+        "build.zig",
+        "build.zig.zon",
+        "src",
+        // For example...
+        //"LICENSE",
+        //"README.md",
+    },
+}
diff --git a/src/benchmark.zig b/src/benchmark.zig
new file mode 100644
index 0000000..4c30609
--- /dev/null
+++ b/src/benchmark.zig
@@ -0,0 +1,55 @@
+const std = @import("std");
+const Random = std.Random;
+const time = std.time;
+const bubble = @import("sort/bubble.zig");
+const selection = @import("sort/selection.zig");
+const insertion = @import("sort/insertion.zig");
+const testing = std.testing;
+
+fn benchmark(comptime T: type, comptime sort_fn: fn (type, []T) void, n: usize, runs: usize) !u64 {
+    var prng = std.rand.DefaultPrng.init(0);
+    var random = prng.random();
+    var total_time: u64 = 0;
+    for (0..runs) |_| {
+        const list = try testing.allocator.alloc(T, n);
+        defer testing.allocator.free(list);
+        for (list) |*item| {
+            item.* = random.int(T);
+        }
+        const start = time.milliTimestamp();
+        sort_fn(T, list);
+        const end = time.milliTimestamp();
+        total_time += @intCast(end - start);
+    }
+    return total_time / runs;
+}
+
+test "bubble sort benchmark" {
+    const runs = 10;
+    const sizes = [_]usize{ 100, 1000, 10000 };
+    std.debug.print("\nBubble Sort Benchmark:\n", .{});
+    for (sizes) |size| {
+        const avg_time = try benchmark(i64, bubble.sort, size, runs);
+        std.debug.print("Average time for N={d}: {d} ms\n", .{ size, avg_time });
+    }
+}
+
+test "selection sort benchmark" {
+    const runs = 10;
+    const sizes = [_]usize{ 100, 1000, 10000 };
+    std.debug.print("\nSelection Sort Benchmark:\n", .{});
+    for (sizes) |size| {
+        const avg_time = try benchmark(i64, selection.sort, size, runs);
+        std.debug.print("Average time for N={d}: {d} ms\n", .{ size, avg_time });
+    }
+}
+
+test "insertion sort benchmark" {
+    const runs = 10;
+    const sizes = [_]usize{ 100, 1000, 10000 };
+    std.debug.print("\nInsertion Sort Benchmark:\n", .{});
+    for (sizes) |size| {
+        const avg_time = try benchmark(i64, insertion.sort, size, runs);
+        std.debug.print("Average time for N={d}: {d} ms\n", .{ size, avg_time });
+    }
+}
diff --git a/src/main.zig b/src/main.zig
new file mode 100644
index 0000000..13ab026
--- /dev/null
+++ b/src/main.zig
@@ -0,0 +1,3 @@
+const std = @import("std");
+
+pub fn main() !void {}
diff --git a/src/root.zig b/src/root.zig
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/src/root.zig
diff --git a/src/sort/bubble.zig b/src/sort/bubble.zig
new file mode 100644
index 0000000..5d868a2
--- /dev/null
+++ b/src/sort/bubble.zig
@@ -0,0 +1,20 @@
+const std = @import("std");
+const mem = std.mem;
+const testing = std.testing;
+
+pub fn sort(comptime T: type, arr: []T) void {
+    for (0..arr.len - 1) |i| {
+        for (i + 1..arr.len) |j| {
+            if (arr[i] > arr[j]) {
+                mem.swap(T, &arr[i], &arr[j]);
+            }
+        }
+    }
+}
+
+test "bubble_sort test" {
+    var arr = [_]i64{ 20, 3, 5, 1, 30, 4, 2 };
+    sort(i64, &arr);
+    const expected = [_]i64{ 1, 2, 3, 4, 5, 20, 30 };
+    try testing.expectEqual(expected, arr);
+}
diff --git a/src/sort/insertion.zig b/src/sort/insertion.zig
new file mode 100644
index 0000000..4aee57a
--- /dev/null
+++ b/src/sort/insertion.zig
@@ -0,0 +1,19 @@
+const std = @import("std");
+const mem = std.mem;
+const testing = std.testing;
+
+pub fn sort(comptime T: type, arr: []T) void {
+    for (1..arr.len) |i| {
+        var j = i;
+        while (j > 0 and arr[j - 1] > arr[j]) : (j -= 1) {
+            mem.swap(T, &arr[j - 1], &arr[j]);
+        }
+    }
+}
+
+test "insertion sort test" {
+    var arr = [_]i64{ 20, 3, 5, 1, 30, 4, 2 };
+    sort(i64, &arr);
+    const expected = [_]i64{ 1, 2, 3, 4, 5, 20, 30 };
+    try testing.expectEqualSlices(i64, &expected, &arr);
+}
diff --git a/src/sort/selection.zig b/src/sort/selection.zig
new file mode 100644
index 0000000..0629680
--- /dev/null
+++ b/src/sort/selection.zig
@@ -0,0 +1,22 @@
+const std = @import("std");
+const mem = std.mem;
+const testing = std.testing;
+
+pub fn sort(comptime T: type, arr: []T) void {
+    for (0..arr.len - 1) |i| {
+        var min = i;
+        for (i + 1..arr.len) |j| {
+            if (arr[min] > arr[j]) {
+                min = j;
+            }
+        }
+        mem.swap(T, &arr[i], &arr[min]);
+    }
+}
+
+test "selection sort test" {
+    var arr = [_]i64{ 20, 3, 5, 1, 30, 4, 2 };
+    sort(i64, &arr);
+    const expected = [_]i64{ 1, 2, 3, 4, 5, 20, 30 };
+    try testing.expectEqual(expected, arr);
+}