about summary refs log tree commit diff
path: root/src/sort
diff options
context:
space:
mode:
authoriurii plugatarov <[email protected]>2024-07-28 21:29:10 +0200
committeriurii plugatarov <[email protected]>2024-07-28 21:29:10 +0200
commit2b4414f8d8496d72ff904606dcf7c600257d3bd8 (patch)
treec1489d65b5ae91c4d96184be0d86083557075c67 /src/sort
parenta679ae0a00adc299ab89efb69fad44ebab777d35 (diff)
downloaddsazitty-2b4414f8d8496d72ff904606dcf7c600257d3bd8.tar.gz
bench improve
Diffstat (limited to 'src/sort')
-rw-r--r--src/sort/shell.zig26
1 files changed, 26 insertions, 0 deletions
diff --git a/src/sort/shell.zig b/src/sort/shell.zig
new file mode 100644
index 0000000..7a2b338
--- /dev/null
+++ b/src/sort/shell.zig
@@ -0,0 +1,26 @@
+const std = @import("std");
+const mem = std.mem;
+const testing = std.testing;
+
+pub fn sort(comptime T: type, arr: []T) void {
+    var gap = arr.len / 2;
+
+    while (gap > 0) : (gap /= 2) {
+        var i: usize = gap;
+        while (i < arr.len) : (i += 1) {
+            const temp = arr[i];
+            var j: usize = i;
+            while (j >= gap and arr[j - gap] > temp) : (j -= gap) {
+                arr[j] = arr[j - gap];
+            }
+            arr[j] = temp;
+        }
+    }
+}
+
+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);
+}