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
Changes from 1 commit
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
Next Next commit
feat: menambahkan algoritma bucket_sort
  • Loading branch information
Bagusdevaa committed Apr 28, 2025
commit f05fc811cda916f7185b14a80d2e7af51c9567f9
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):
"""
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)}")