summaryrefslogtreecommitdiff
path: root/algorithms/insertion_sort.py
diff options
context:
space:
mode:
Diffstat (limited to 'algorithms/insertion_sort.py')
-rwxr-xr-xalgorithms/insertion_sort.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/algorithms/insertion_sort.py b/algorithms/insertion_sort.py
new file mode 100755
index 0000000..ab786e0
--- /dev/null
+++ b/algorithms/insertion_sort.py
@@ -0,0 +1,20 @@
+#!/usr/bin/env python2
+
+import random
+
+
+def insertion_sort(l):
+ sz = len(l)
+ for i in range(sz):
+ if i == 0: continue
+ j = i-1
+ while l[i] < l[j] and j > -1:
+ j -= 1
+ l.insert(j+1, l.pop(i))
+ return l
+
+l = list(reversed(range(1000)))
+#random.shuffle(l)
+print l
+print insertion_sort(l)
+