Skip to content

chore: menambahkan gnome sorting #332

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 14, 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
1 change: 1 addition & 0 deletions DIRECTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@
* [Bubble Sort Ascii](https://github.com/bellshade/Python/blob/main/algorithm/sorting/bubble_sort_ascii.py)
* [Bucket Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/bucket_sort.py)
* [Circle Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/circle_sort.py)
* [Gnome Sorting](https://github.com/bellshade/Python/blob/main/algorithm/sorting/gnome_sorting.py)
* [Merge Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/merge_sort.py)
* [Pop Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/pop_sort.py)
* [Quick Sorting](https://github.com/bellshade/Python/blob/main/algorithm/sorting/quick_sorting.py)
Expand Down
38 changes: 38 additions & 0 deletions algorithm/sorting/gnome_sorting.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def gnome_sorting(daftar: list) -> list:
"""
Implementasi dari algoritma gnome sorting

fungsi yang menerima daftar yang dapat nantinya diubah
dan diurut dengan elemen heterogen yang bisa dibandingkan,
lalu mengembalikan list tersebut dalam urutan menaik

Parameter:
daftar (list): data yang ingin diberikan

Return:
(list): hasil sorting gnome

Contoh:
>>> gnome_sorting([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
"""
if len(daftar) <= 1:
return daftar

i = 1
while i < len(daftar):
if daftar[i - 1] <= daftar[i]:
i += 1
else:
# tukar elemen jika tidak dalam urutan yang bener
daftar[i - 1], daftar[i] = daftar[i], daftar[i - 1]
i -= 1
if i == 0:
i = 1
return daftar


if __name__ == "__main__":
import doctest

doctest.testmod(verbose=True)
Loading