From f05fc811cda916f7185b14a80d2e7af51c9567f9 Mon Sep 17 00:00:00 2001 From: I Kadek Bagus Deva Diga Dana Putra <114275908+Bagusdevaa@users.noreply.github.com> Date: Mon, 28 Apr 2025 05:04:45 +0000 Subject: [PATCH 1/4] feat: menambahkan algoritma bucket_sort --- algorithm/sorting/bucket_sort.py | 50 ++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 algorithm/sorting/bucket_sort.py diff --git a/algorithm/sorting/bucket_sort.py b/algorithm/sorting/bucket_sort.py new file mode 100644 index 00000000..fe8a70ca --- /dev/null +++ b/algorithm/sorting/bucket_sort.py @@ -0,0 +1,50 @@ +# Bucket Sort adalah algoritma pengurutan +# yang membagi array ke dalam beberapa bucket, +# lalu setiap bucket diurutkan secara individu +# dan akhirnya semua bucket digabungkan menjadi array akhir. + +# Distribusikan elemen ke dalam bucket. +# Urutkan masing-masing bucket. +# Gabungkan semua bucket menjadi satu list terurut. + +def bucket_sort(collection): + """ + contoh + >>> bucket_sort([0.25, 0.36, 0.58, 0.41, 0.29, 0.22, 0.45, 0.79]) + [0.22, 0.25, 0.29, 0.36, 0.41, 0.45, 0.58, 0.79] + >>> bucket_sort([0.5, 0.3, 0.9, 0.7]) + [0.3, 0.5, 0.7, 0.9] + """ + + if len(collection) == 0: + return collection + + # Membuat bucket kosong + bucket_count = len(collection) + buckets = [[] for _ in range(bucket_count)] + + # Menempatkan elemen ke dalam bucket + for value in collection: + index = int(value * bucket_count) + if index != bucket_count: + buckets[index].append(value) + else: + buckets[bucket_count - 1].append(value) + + # Mengurutkan setiap bucket dan menggabungkannya + sorted_array = [] + for bucket in buckets: + sorted_array.extend(sorted(bucket)) + + return sorted_array + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) + + data = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68] + unsorted = [float(item) for item in data] + print(f"data yang belum di sorting adalah {unsorted}") + print(f"data yang sudah di sorting {bucket_sort(unsorted)}") From b76a4f5579bbbc1ae77093af6833d3a1b902296d Mon Sep 17 00:00:00 2001 From: I Kadek Bagus Deva Diga Dana Putra <114275908+Bagusdevaa@users.noreply.github.com> Date: Mon, 28 Apr 2025 05:04:59 +0000 Subject: [PATCH 2/4] feat: menambahkan algoritma circle_sort --- algorithm/sorting/circle_sort.py | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 algorithm/sorting/circle_sort.py diff --git a/algorithm/sorting/circle_sort.py b/algorithm/sorting/circle_sort.py new file mode 100644 index 00000000..7193d9b7 --- /dev/null +++ b/algorithm/sorting/circle_sort.py @@ -0,0 +1,51 @@ +# Circle Sort adalah algoritma pengurutan +# berbasis rekursif yang membandingkan dan +# menukar elemen-elemen dari ujung ke tengah, +# berulang sampai array tersortir. + +# Bandingkan elemen paling kiri dengan paling kanan. +# Jika elemen kiri lebih besar, tukar dengan elemen kanan. +# Lanjutkan ke tengah array, lalu lakukan rekursi. + +def circle_sort(collection): + """ + contoh + >>> circle_sort([5, 3, 2, 8, 1, 4]) + [1, 2, 3, 4, 5, 8] + >>> circle_sort([10, 20, -5, 7, 3]) + [-5, 3, 7, 10, 20] + """ + + def circle_sort_rec(arr, left, right): + if left == right: + return False + swapped = False + l, r = left, right + while l < r: + if arr[l] > arr[r]: + arr[l], arr[r] = arr[r], arr[l] + swapped = True + l += 1 + r -= 1 + if l == r and arr[l] > arr[r + 1]: + arr[l], arr[r + 1] = arr[r + 1], arr[l] + swapped = True + mid = (right - left) // 2 + left + left_swapped = circle_sort_rec(arr, left, mid) + right_swapped = circle_sort_rec(arr, mid + 1, right) + return swapped or left_swapped or right_swapped + + while circle_sort_rec(collection, 0, len(collection) - 1): + pass + return collection + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) + + data = [10, -2, 7, 4, 3] + unsorted = [int(item) for item in data] + print(f"data yang belum di sorting adalah {unsorted}") + print(f"data yang sudah di sorting {circle_sort(unsorted)}") From eb5847c6b6e25ac2f126244d601996f8cd489f5f Mon Sep 17 00:00:00 2001 From: Bagusdevaa Date: Sun, 4 May 2025 14:33:20 +0800 Subject: [PATCH 3/4] fix: Menambahkan type hinting ke dalam algoritma bucket sort --- algorithm/sorting/bucket_sort.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/algorithm/sorting/bucket_sort.py b/algorithm/sorting/bucket_sort.py index fe8a70ca..56a2a37f 100644 --- a/algorithm/sorting/bucket_sort.py +++ b/algorithm/sorting/bucket_sort.py @@ -7,7 +7,7 @@ # Urutkan masing-masing bucket. # Gabungkan semua bucket menjadi satu list terurut. -def bucket_sort(collection): +def bucket_sort(collection: list[float]) -> list[float]: """ contoh >>> bucket_sort([0.25, 0.36, 0.58, 0.41, 0.29, 0.22, 0.45, 0.79]) @@ -20,19 +20,19 @@ def bucket_sort(collection): return collection # Membuat bucket kosong - bucket_count = len(collection) - buckets = [[] for _ in range(bucket_count)] + bucket_count: int = len(collection) + buckets: list[list[float]] = [[] for _ in range(bucket_count)] # Menempatkan elemen ke dalam bucket for value in collection: - index = int(value * bucket_count) + index: int = int(value * bucket_count) if index != bucket_count: buckets[index].append(value) else: buckets[bucket_count - 1].append(value) # Mengurutkan setiap bucket dan menggabungkannya - sorted_array = [] + sorted_array: list[float] = [] for bucket in buckets: sorted_array.extend(sorted(bucket)) @@ -44,7 +44,7 @@ def bucket_sort(collection): doctest.testmod(verbose=True) - data = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68] - unsorted = [float(item) for item in data] + data: list[float] = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68] + unsorted: list[float] = [float(item) for item in data] print(f"data yang belum di sorting adalah {unsorted}") print(f"data yang sudah di sorting {bucket_sort(unsorted)}") From 8a554a04f1f21afc2ea6d78bb61cbcad6bc54208 Mon Sep 17 00:00:00 2001 From: Bagusdevaa Date: Sun, 4 May 2025 14:33:49 +0800 Subject: [PATCH 4/4] fix: Menambahkan type hinting ke algoritma circle_sort --- algorithm/sorting/circle_sort.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/algorithm/sorting/circle_sort.py b/algorithm/sorting/circle_sort.py index 7193d9b7..d58c7460 100644 --- a/algorithm/sorting/circle_sort.py +++ b/algorithm/sorting/circle_sort.py @@ -7,7 +7,7 @@ # Jika elemen kiri lebih besar, tukar dengan elemen kanan. # Lanjutkan ke tengah array, lalu lakukan rekursi. -def circle_sort(collection): +def circle_sort(collection: list[int]) -> list[int]: """ contoh >>> circle_sort([5, 3, 2, 8, 1, 4]) @@ -16,7 +16,7 @@ def circle_sort(collection): [-5, 3, 7, 10, 20] """ - def circle_sort_rec(arr, left, right): + def circle_sort_rec(arr: list[int], left: int, right: int) -> bool: if left == right: return False swapped = False @@ -45,7 +45,7 @@ def circle_sort_rec(arr, left, right): doctest.testmod(verbose=True) - data = [10, -2, 7, 4, 3] - unsorted = [int(item) for item in data] + data: list[int] = [10, -2, 7, 4, 3] + unsorted: list[int] = [int(item) for item in data] print(f"data yang belum di sorting adalah {unsorted}") print(f"data yang sudah di sorting {circle_sort(unsorted)}")