-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: menambahkan algoritma bubble sorting
Signed-off-by: slowy07 <slowy.arfy@gmail.com>
- Loading branch information
Showing
3 changed files
with
56 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const std = @import("std"); | ||
const builtin = std.builtin; | ||
|
||
// fungsi untuk melakukan pengurutan pada array A secara naik | ||
pub fn bubleSorting(A: []i32) void { | ||
var n = A.len; | ||
while (n > 1) { | ||
var new_n: usize = 0; | ||
for (A[0 .. n - 1], 0..) |value, i| { | ||
// jika nilai saat ini lebih besar dari nilai berikutnya, tukar posisi | ||
if (value > A[i + 1]) { | ||
std.mem.swap(i32, &A[i], &A[i + 1]); | ||
new_n = i + 1; | ||
} | ||
} | ||
n = new_n; | ||
} | ||
} | ||
|
||
// testing jika array nya adalah kosong | ||
test "array kosong" { | ||
var array: [1]i32 = .{5}; | ||
bubleSorting(&array); | ||
const a = array.len; | ||
try std.testing.expect(a == 1); | ||
try std.testing.expect(array[0] == 5); | ||
} | ||
|
||
// testing untuk array dengan satu elemen | ||
test "array satu elemen" { | ||
var array: [1]i32 = .{5}; | ||
bubleSorting(&array); | ||
const a = array.len; | ||
try std.testing.expect(a == 1); | ||
try std.testing.expect(array[0] == 5); | ||
} | ||
|
||
// testing array yang tidak terurut | ||
test "array tidak terurut" { | ||
var array: [10]i32 = .{ 2, 1, 3, 4, 5, 6, 7, 9, 8, 10 }; | ||
bubleSorting(&array); | ||
for (array, 0..) |value, i| { | ||
try std.testing.expect(value == (i + 1)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters