diff options
author | iurii plugatarov <[email protected]> | 2024-07-28 21:07:00 +0200 |
---|---|---|
committer | iurii plugatarov <[email protected]> | 2024-07-28 21:07:00 +0200 |
commit | a679ae0a00adc299ab89efb69fad44ebab777d35 (patch) | |
tree | 243d4ce9ea0effe6fcf89e61cee80b10b562925d /src/sort/bubble.zig | |
download | dsazitty-a679ae0a00adc299ab89efb69fad44ebab777d35.tar.gz |
init
Diffstat (limited to 'src/sort/bubble.zig')
-rw-r--r-- | src/sort/bubble.zig | 20 |
1 files changed, 20 insertions, 0 deletions
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); +} |