Skip to content

Commit

Permalink
feat: menambahkan sorting merge sorting
Browse files Browse the repository at this point in the history
memperbaiki unitesting

Signed-off-by: slowy07 <slowy.arfy@gmail.com>
  • Loading branch information
slowy07 committed Dec 13, 2023
1 parent d10a0fa commit 8906082
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 4 deletions.
4 changes: 0 additions & 4 deletions .github/workflows/zig.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@ jobs:
version: 0.11.0
cache: false


- name: format kode zig
run: zig fmt .

- name: testing kode zig
if: (startsWith(matrix.runs-on, 'ubuntu')) || (startsWith(matrix.runs-on, 'macos'))
run: sh runtest.sh
Expand Down
70 changes: 70 additions & 0 deletions algorithm/sorting/mergeSort.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const std = @import("std");

// Fungsi sorting untuk mengurutkan array dari variable A dengan merge sorting
pub fn mergeSort(array: []i32, b: []i32) void {
// cek panjang array a dan b sama
std.debug.assert(array.len == b.len);
// salin elemen-elemen dari array ke b
salinArray(array, 0, array.len, b);
// jalankan merge sort
bagiArray(b, 0, array.len, array);
}

// fungsi untuk membagi dan menggabungkan array
fn bagiArray(b: []i32, start: usize, end: usize, array: []i32) void {
// jika panjang array kurang dari atau sama dengan 1, maka
// array sudah terurut
if (end - start <= 1) {
return;
}
// mencari titik tengah array
var mid = (end + start) / 2;
// panggil fungsi bagiArray secara rekursif dua bagian array
bagiArray(array, start, mid, b);
bagiArray(array, mid, end, b);
// gabung dua baguan array yang sudah terurut
gabungArray(b, start, mid, end, array);
}

// fungsi menggabungkan dua bagian array yang terurut
fn gabungArray(array: []i32, start: usize, mid: usize, end: usize, b: []i32) void {
var i = start;
var k = start;
var j = mid;
// gabunggang dua bagian array
while (k < end) : (k += 1) {
if (i < mid and (j >= end or array[i] <= array[j])) {
b[k] = array[i];
i = i + 1;
} else {
b[k] = array[j];
j = j + 1;
}
}
}

// fungsi salin_array untuk menyalin elemen array dari satu array ke array lainnya
fn salinArray(array: []i32, start: usize, end: usize, b: []i32) void {
var k = start;
while (k < end) : (k += 1) {
b[k] = array[k];
}
}

// buat testing untuk memastikan fungsi mergeSort berjalan
test "testing mergeSorting" {
// test array kosong
var array: []i32 = &.{};
var w_array: []i32 = &.{};
mergeSort(array, w_array);
const a = array.len;
try std.testing.expect(a == 0);

// testing dengan array terbalik
var array_terbalik: [10]i32 = .{ 10, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
var w_array_terbalik: [10]i32 = .{0} ** 10;
mergeSort(&array_terbalik, &w_array_terbalik);
for (array_terbalik, 0..) |value, i| {
try std.testing.expect(value == (i + 1));
}
}
12 changes: 12 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ pub fn build(b: *std.Build) void {
.category = "math",
});

// math/ceil
// ceil
if (std.mem.eql(u8, op, "math/ceil"))
buat_algo(b, .{
.optimize = optimize,
Expand All @@ -45,6 +47,16 @@ pub fn build(b: *std.Build) void {
.name = "bubbleSort.zig",
.category = "algorithm/sorting",
});

// algoritma/sorting
// merge sorting
if (std.mem.eql(u8, op, "algorithm/sorting/mergeSort"))
buat_algo(b, .{
.optimize = optimize,
.target = target,
.name = "mergeSort.zig",
.category = "algorithm/sorting",
});
}

// fungsi untuk membangun algoritma berdasarkan informasi yang diberikan
Expand Down
1 change: 1 addition & 0 deletions runtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ $ZIG_TEST_COMMAND -Dalgoritma=math/gcd $argumen
$ZIG_TEST_COMMAND -Dalgoritma=math/faktorial $argumen
$ZIG_TEST_COMMAND -Dalgoritma=math/ceil $argumen
$ZIG_TEST_COMMAND -Dalgoritma=algorithm/sorting/bubbleSort $argumen
$ZIG_TEST_COMMAND -Dalgoritma=algorithm/sorting/mergeSort $argumen

0 comments on commit 8906082

Please sign in to comment.