Skip to content

Commit 664a758

Browse files
committed
gnome sort added
1 parent 98cf034 commit 664a758

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

algorithms/sorting/gnome_sort.py

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
"""
2+
gnome_sort.py
3+
4+
Implementation of gnome sort on a list and returns a sorted list.
5+
6+
Gnome Sort Overview:
7+
---------------------
8+
A sorting algorithm similar to insertion sort except that the element is moved to its proper place by a series of swaps.
9+
10+
Time Complexity: O(n^2)
11+
12+
Space Complexity: O(1) auxillary
13+
14+
Stable: No
15+
16+
Psuedo code: http://en.wikipedia.org/wiki/Gnome_sort
17+
18+
"""
19+
20+
21+
def sort(seq):
22+
23+
i = 1
24+
while i < len(seq):
25+
if seq[i] < seq[i-1]:
26+
seq[i], seq[i-1] = seq[i-1], seq[i]
27+
if i > 1:
28+
i -= 1
29+
else:
30+
i += 1
31+
return seq
32+
33+
print sort([11, 14, 11, -1, 24, -12343, -0.34, 123.22, 12, 14, 23,33])

0 commit comments

Comments
 (0)