Skip to content

Penambahan Implementasi Algoritma Circle Sort dan Bucket Sort #331

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 50 additions & 0 deletions algorithm/sorting/bucket_sort.py
Original file line number Diff line number Diff line change
@@ -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)}")
51 changes: 51 additions & 0 deletions algorithm/sorting/circle_sort.py
Original file line number Diff line number Diff line change
@@ -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)}")
Loading