Skip to content

Commit fb070bd

Browse files
slowy07github-actions
andauthored
chore: menambahkan gnome sorting (#332)
* chore: menambahkan fungsi gamma fungsi gamma adalah generalisasi dari fungsi faktorial ke bilangan ril dan bilangan kompleks Signed-off-by: slowy07 <slowy.arfy@proton.me> * chore: menambahkan algoritma gnome sorting Signed-off-by: slowy07 <slowy.arfy@proton.me> * docs: update DIRECTORY.md --------- Signed-off-by: slowy07 <slowy.arfy@proton.me> Co-authored-by: github-actions <bellshdae07@gmail.com>
1 parent bfdfe0a commit fb070bd

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
* [Bubble Sort Ascii](https://github.com/bellshade/Python/blob/main/algorithm/sorting/bubble_sort_ascii.py)
150150
* [Bucket Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/bucket_sort.py)
151151
* [Circle Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/circle_sort.py)
152+
* [Gnome Sorting](https://github.com/bellshade/Python/blob/main/algorithm/sorting/gnome_sorting.py)
152153
* [Merge Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/merge_sort.py)
153154
* [Pop Sort](https://github.com/bellshade/Python/blob/main/algorithm/sorting/pop_sort.py)
154155
* [Quick Sorting](https://github.com/bellshade/Python/blob/main/algorithm/sorting/quick_sorting.py)

algorithm/sorting/gnome_sorting.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
def gnome_sorting(daftar: list) -> list:
2+
"""
3+
Implementasi dari algoritma gnome sorting
4+
5+
fungsi yang menerima daftar yang dapat nantinya diubah
6+
dan diurut dengan elemen heterogen yang bisa dibandingkan,
7+
lalu mengembalikan list tersebut dalam urutan menaik
8+
9+
Parameter:
10+
daftar (list): data yang ingin diberikan
11+
12+
Return:
13+
(list): hasil sorting gnome
14+
15+
Contoh:
16+
>>> gnome_sorting([0, 5, 3, 2, 2])
17+
[0, 2, 2, 3, 5]
18+
"""
19+
if len(daftar) <= 1:
20+
return daftar
21+
22+
i = 1
23+
while i < len(daftar):
24+
if daftar[i - 1] <= daftar[i]:
25+
i += 1
26+
else:
27+
# tukar elemen jika tidak dalam urutan yang bener
28+
daftar[i - 1], daftar[i] = daftar[i], daftar[i - 1]
29+
i -= 1
30+
if i == 0:
31+
i = 1
32+
return daftar
33+
34+
35+
if __name__ == "__main__":
36+
import doctest
37+
38+
doctest.testmod(verbose=True)

0 commit comments

Comments
 (0)