Created gnome sort in sort algorithm (#313) · wxpython/algorithms@36c1af4 · GitHub
Skip to content

Commit 36c1af4

Browse files
geon0325goswami-rahul
authored andcommitted
Created gnome sort in sort algorithm (keon#313)
* Create gnome_sort.py * Update __init__.py * Update test_sort.py * Update README.md * Update README_CN.md * Update README_GE.md * Update README_JP.md * Update README_KR.md * Update gnome_sort.py
1 parent 537e4fb commit 36c1af4

8 files changed

Lines changed: 32 additions & 2 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions

README_CN.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ pip3 uninstall -y algorithms
212212
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
213213
- [comb_sort:梳排序](algorithms/sort/comb_sort.py)
214214
- [counting_sort:计数排序](algorithms/sort/counting_sort.py)
215+
- [gnome_sort](algorithms/sort/gnome_sort.py)
215216
- [heap_sort:堆排序](algorithms/sort/heap_sort.py)
216217
- [insertion_sort:插入排序](algorithms/sort/insertion_sort.py)
217218
- [meeting_rooms:会议室](algorithms/sort/meeting_rooms.py)

README_GE.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
233233
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
234234
- [comb_sort](algorithms/sort/comb_sort.py)
235235
- [counting_sort](algorithms/sort/counting_sort.py)
236+
- [gnome_sort](algorithms/sort/gnome_sort.py)
236237
- [heap_sort](algorithms/sort/heap_sort.py)
237238
- [insertion_sort](algorithms/sort/insertion_sort.py)
238239
- [meeting_rooms](algorithms/sort/meeting_rooms.py)

README_JP.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ if __name__ == "__main__":
227227
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
228228
- [comb_sort](algorithms/sort/comb_sort.py)
229229
- [counting_sort](algorithms/sort/counting_sort.py)
230+
- [gnome_sort](algorithms/sort/gnome_sort.py)
230231
- [heap_sort](algorithms/sort/heap_sort.py)
231232
- [insertion_sort](algorithms/sort/insertion_sort.py)
232233
- [meeting_rooms](algorithms/sort/meeting_rooms.py)

README_KR.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ if __name__ == "__main__":
223223
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
224224
- [comb_sort](algorithms/sort/comb_sort.py)
225225
- [counting_sort](algorithms/sort/counting_sort.py)
226+
- [gnome_sort](algorithms/sort/gnome_sort.py)
226227
- [heap_sort](algorithms/sort/heap_sort.py)
227228
- [insertion_sort](algorithms/sort/insertion_sort.py)
228229
- [meeting_rooms](algorithms/sort/meeting_rooms.py)

algorithms/sort/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@
1111
from .bucket_sort import *
1212
from .shell_sort import *
1313
from .radix_sort import *
14+
from .gnome_sort import *
1415
from .cocktail_shaker_sort import *

algorithms/sort/gnome_sort.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
3+
Gnome Sort
4+
Best case performance is O(n)
5+
Worst case performance is O(n^2)
6+
7+
"""
8+
9+
10+
def gnome_sort(arr):
11+
n = len(arr)
12+
index = 0
13+
while index < n:
14+
if index == 0 or arr[index] >= arr[index-1]:
15+
index = index + 1
16+
else:
17+
arr[index], arr[index-1] = arr[index-1], arr[index]
18+
index = index - 1
19+
return arr

tests/test_sort.py

Lines changed: 7 additions & 2 deletions

0 commit comments

Comments
 (0)