Skip to content

Commit

Permalink
Created gnome sort in sort algorithm (#313)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
geon0325 authored and goswami-rahul committed Jun 6, 2018
1 parent 537e4fb commit 36c1af4
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ If you want to uninstall algorithms, it is as simple as:
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
- [insertion_sort](algorithms/sort/insertion_sort.py)
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
Expand Down
1 change: 1 addition & 0 deletions README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ pip3 uninstall -y algorithms
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort:梳排序](algorithms/sort/comb_sort.py)
- [counting_sort:计数排序](algorithms/sort/counting_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort:堆排序](algorithms/sort/heap_sort.py)
- [insertion_sort:插入排序](algorithms/sort/insertion_sort.py)
- [meeting_rooms:会议室](algorithms/sort/meeting_rooms.py)
Expand Down
1 change: 1 addition & 0 deletions README_GE.md
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ Um das Projekt zu deinstallieren tippen Sie folgendes:
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
- [insertion_sort](algorithms/sort/insertion_sort.py)
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
Expand Down
1 change: 1 addition & 0 deletions README_JP.md
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ if __name__ == "__main__":
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
- [insertion_sort](algorithms/sort/insertion_sort.py)
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
Expand Down
1 change: 1 addition & 0 deletions README_KR.md
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,7 @@ if __name__ == "__main__":
- [cocktail_shaker_sort](algorithms/sort/cocktail_shaker_sort.py)
- [comb_sort](algorithms/sort/comb_sort.py)
- [counting_sort](algorithms/sort/counting_sort.py)
- [gnome_sort](algorithms/sort/gnome_sort.py)
- [heap_sort](algorithms/sort/heap_sort.py)
- [insertion_sort](algorithms/sort/insertion_sort.py)
- [meeting_rooms](algorithms/sort/meeting_rooms.py)
Expand Down
1 change: 1 addition & 0 deletions algorithms/sort/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
from .bucket_sort import *
from .shell_sort import *
from .radix_sort import *
from .gnome_sort import *
from .cocktail_shaker_sort import *
19 changes: 19 additions & 0 deletions algorithms/sort/gnome_sort.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
"""
Gnome Sort
Best case performance is O(n)
Worst case performance is O(n^2)
"""


def gnome_sort(arr):
n = len(arr)
index = 0
while index < n:
if index == 0 or arr[index] >= arr[index-1]:
index = index + 1
else:
arr[index], arr[index-1] = arr[index-1], arr[index]
index = index - 1
return arr
9 changes: 7 additions & 2 deletions tests/test_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
bucket_sort,
shell_sort,
radix_sort,
cocktail_shaker_sort
gnome_sort,
cocktail_shaker_sort,
)

import unittest
Expand Down Expand Up @@ -69,7 +70,11 @@ def test_shell_sort(self):
def test_radix_sort(self):
self.assertEqual([1, 5, 23, 57, 65, 1232],
radix_sort([1, 5, 65, 23, 57, 1232]))


def test_gnome_sort(self):
self.assertEqual([1, 5, 23, 57, 65, 1232],
gnome_sort([1, 5, 65, 23, 57, 1232]))

def test_cocktail_shaker_sort(self):
self.assertEqual([1, 5, 23, 57, 65, 1232],
cocktail_shaker_sort([1, 5, 65, 23, 57, 1232]))
Expand Down

0 comments on commit 36c1af4

Please sign in to comment.