-
-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: menambahkan algoritma sorting bead sort
Signed-off-by: slowy07 <[email protected]>
- Loading branch information
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# beard sort atau juga bisa disebut dengan gravity sort | ||
# algoritma sorting ini terinspirasi dari fenomena alam | ||
# dan dirancang dengan mengingat object yang jatuh dibawah | ||
# pengaruh gravitasi | ||
|
||
# beard sort hanya berfungsi untuk urutan bilangan | ||
# bilat non-negatif | ||
|
||
|
||
def bead_sort(sequence: list) -> list: | ||
""" | ||
>>> bead_sort([6, 11, 12, 4, 1, 5]) | ||
[1, 4, 5, 6, 11, 12] | ||
>>> bead_sort("coba string") | ||
Traceback (most recent call last): | ||
... | ||
TypeError: Sequence harus berupa list dari non negatif | ||
""" | ||
if any(not isinstance(x, int) or x < 0 for x in sequence): | ||
raise TypeError("Sequence harus berupa list dari non negatif") | ||
for _ in range(len(sequence)): | ||
for i, (rod_upper, rod_lower) in enumerate(zip(sequence, sequence[1:])): | ||
if rod_upper > rod_lower: | ||
sequence[i] -= rod_upper - rod_lower | ||
sequence[i + 1] += rod_upper - rod_lower | ||
return sequence | ||
|
||
|
||
if __name__ == "__main__": | ||
assert bead_sort([5, 4, 3, 2, 1]) == [1, 2, 3, 4, 5] |