Skip to content

Commit 2d32ec5

Browse files
authored
Penambahan Implementasi Algoritma Circle Sort dan Bucket Sort (#331)
* feat: menambahkan algoritma bucket_sort * feat: menambahkan algoritma circle_sort * fix: Menambahkan type hinting ke dalam algoritma bucket sort * fix: Menambahkan type hinting ke algoritma circle_sort
1 parent ead3ef7 commit 2d32ec5

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

algorithm/sorting/bucket_sort.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Bucket Sort adalah algoritma pengurutan
2+
# yang membagi array ke dalam beberapa bucket,
3+
# lalu setiap bucket diurutkan secara individu
4+
# dan akhirnya semua bucket digabungkan menjadi array akhir.
5+
6+
# Distribusikan elemen ke dalam bucket.
7+
# Urutkan masing-masing bucket.
8+
# Gabungkan semua bucket menjadi satu list terurut.
9+
10+
def bucket_sort(collection: list[float]) -> list[float]:
11+
"""
12+
contoh
13+
>>> bucket_sort([0.25, 0.36, 0.58, 0.41, 0.29, 0.22, 0.45, 0.79])
14+
[0.22, 0.25, 0.29, 0.36, 0.41, 0.45, 0.58, 0.79]
15+
>>> bucket_sort([0.5, 0.3, 0.9, 0.7])
16+
[0.3, 0.5, 0.7, 0.9]
17+
"""
18+
19+
if len(collection) == 0:
20+
return collection
21+
22+
# Membuat bucket kosong
23+
bucket_count: int = len(collection)
24+
buckets: list[list[float]] = [[] for _ in range(bucket_count)]
25+
26+
# Menempatkan elemen ke dalam bucket
27+
for value in collection:
28+
index: int = int(value * bucket_count)
29+
if index != bucket_count:
30+
buckets[index].append(value)
31+
else:
32+
buckets[bucket_count - 1].append(value)
33+
34+
# Mengurutkan setiap bucket dan menggabungkannya
35+
sorted_array: list[float] = []
36+
for bucket in buckets:
37+
sorted_array.extend(sorted(bucket))
38+
39+
return sorted_array
40+
41+
42+
if __name__ == "__main__":
43+
import doctest
44+
45+
doctest.testmod(verbose=True)
46+
47+
data: list[float] = [0.78, 0.17, 0.39, 0.26, 0.72, 0.94, 0.21, 0.12, 0.23, 0.68]
48+
unsorted: list[float] = [float(item) for item in data]
49+
print(f"data yang belum di sorting adalah {unsorted}")
50+
print(f"data yang sudah di sorting {bucket_sort(unsorted)}")

algorithm/sorting/circle_sort.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Circle Sort adalah algoritma pengurutan
2+
# berbasis rekursif yang membandingkan dan
3+
# menukar elemen-elemen dari ujung ke tengah,
4+
# berulang sampai array tersortir.
5+
6+
# Bandingkan elemen paling kiri dengan paling kanan.
7+
# Jika elemen kiri lebih besar, tukar dengan elemen kanan.
8+
# Lanjutkan ke tengah array, lalu lakukan rekursi.
9+
10+
def circle_sort(collection: list[int]) -> list[int]:
11+
"""
12+
contoh
13+
>>> circle_sort([5, 3, 2, 8, 1, 4])
14+
[1, 2, 3, 4, 5, 8]
15+
>>> circle_sort([10, 20, -5, 7, 3])
16+
[-5, 3, 7, 10, 20]
17+
"""
18+
19+
def circle_sort_rec(arr: list[int], left: int, right: int) -> bool:
20+
if left == right:
21+
return False
22+
swapped = False
23+
l, r = left, right
24+
while l < r:
25+
if arr[l] > arr[r]:
26+
arr[l], arr[r] = arr[r], arr[l]
27+
swapped = True
28+
l += 1
29+
r -= 1
30+
if l == r and arr[l] > arr[r + 1]:
31+
arr[l], arr[r + 1] = arr[r + 1], arr[l]
32+
swapped = True
33+
mid = (right - left) // 2 + left
34+
left_swapped = circle_sort_rec(arr, left, mid)
35+
right_swapped = circle_sort_rec(arr, mid + 1, right)
36+
return swapped or left_swapped or right_swapped
37+
38+
while circle_sort_rec(collection, 0, len(collection) - 1):
39+
pass
40+
return collection
41+
42+
43+
if __name__ == "__main__":
44+
import doctest
45+
46+
doctest.testmod(verbose=True)
47+
48+
data: list[int] = [10, -2, 7, 4, 3]
49+
unsorted: list[int] = [int(item) for item in data]
50+
print(f"data yang belum di sorting adalah {unsorted}")
51+
print(f"data yang sudah di sorting {circle_sort(unsorted)}")

0 commit comments

Comments
 (0)