Pure implementation of the gnome sort algorithm in Python.
(unsorted)
| 1 | from __future__ import print_function |
| 2 | |
| 3 | def gnome_sort(unsorted): |
| 4 | """ |
| 5 | Pure implementation of the gnome sort algorithm in Python. |
| 6 | """ |
| 7 | if len(unsorted) <= 1: |
| 8 | return unsorted |
| 9 | |
| 10 | i = 1 |
| 11 | |
| 12 | while i < len(unsorted): |
| 13 | if unsorted[i-1] <= unsorted[i]: |
| 14 | i += 1 |
| 15 | else: |
| 16 | unsorted[i-1], unsorted[i] = unsorted[i], unsorted[i-1] |
| 17 | i -= 1 |
| 18 | if (i == 0): |
| 19 | i = 1 |
| 20 | |
| 21 | if __name__ == '__main__': |
| 22 | try: |