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

File tree

8 files changed

+32
-2
lines changed

8 files changed

+32
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ If you want to uninstall algorithms, it is as simple as:
228228
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
229229
- [comb_sort](algorithms/sort/comb_sort.py)
230230
- [counting_sort](algorithms/sort/counting_sort.py)
231+
- [gnome_sort](algorithms/sort/gnome_sort.py)
231232
- [heap_sort](algorithms/sort/heap_sort.py)
232233
- [insertion_sort](algorithms/sort/insertion_sort.py)
233234
- [meeting_rooms](algorithms/sort/meeting_rooms.py)

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
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@
1111
bucket_sort,
1212
shell_sort,
1313
radix_sort,
14-
cocktail_shaker_sort
14+
gnome_sort,
15+
cocktail_shaker_sort,
1516
)
1617

1718
import unittest
@@ -69,7 +70,11 @@ def test_shell_sort(self):
6970
def test_radix_sort(self):
7071
self.assertEqual([1, 5, 23, 57, 65, 1232],
7172
radix_sort([1, 5, 65, 23, 57, 1232]))
72-
73+
74+
def test_gnome_sort(self):
75+
self.assertEqual([1, 5, 23, 57, 65, 1232],
76+
gnome_sort([1, 5, 65, 23, 57, 1232]))
77+
7378
def test_cocktail_shaker_sort(self):
7479
self.assertEqual([1, 5, 23, 57, 65, 1232],
7580
cocktail_shaker_sort([1, 5, 65, 23, 57, 1232]))

0 commit comments

Comments
 (0)