diff --git a/algorithm/sorting/bucket_sort.py b/algorithm/sorting/bucket_sort.py new file mode 100644 index 00000000..56a2a37f --- /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: list[float]) -> list[float]: + """ + 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: int = len(collection) + buckets: list[list[float]] = [[] for _ in range(bucket_count)] + + # Menempatkan elemen ke dalam bucket + for value in collection: + 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: list[float] = [] + for bucket in buckets: + sorted_array.extend(sorted(bucket)) + + return sorted_array + + +if __name__ == "__main__": + import doctest + + doctest.testmod(verbose=True) + + 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)}") diff --git a/algorithm/sorting/circle_sort.py b/algorithm/sorting/circle_sort.py new file mode 100644 index 00000000..d58c7460 --- /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: list[int]) -> list[int]: + """ + 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: list[int], left: int, right: int) -> bool: + 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: 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)}")