Skip to content

Commit f9823d7

Browse files
committed
Added Insertion Sort
1 parent 0eb9ede commit f9823d7

File tree

1 file changed

+184
-0
lines changed

1 file changed

+184
-0
lines changed

Sorting/3. Insertion_Sort.ipynb

Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# Insertion Sort"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 17,
13+
"metadata": {
14+
"collapsed": true
15+
},
16+
"outputs": [],
17+
"source": [
18+
"def insertion_sort(array):\n",
19+
" global iterations\n",
20+
" iterations = 0\n",
21+
" for i in range(1, len(array)):\n",
22+
" current_value = array[i]\n",
23+
" for j in range(i - 1, -1, -1):\n",
24+
" iterations += 1\n",
25+
" if array[j] > current_value:\n",
26+
" array[j], array[j + 1] = array[j + 1], array[j] # swap\n",
27+
" else:\n",
28+
" array[j + 1] = current_value\n",
29+
" break"
30+
]
31+
},
32+
{
33+
"cell_type": "markdown",
34+
"metadata": {},
35+
"source": [
36+
"### Time Complexity:\n",
37+
"\n",
38+
"- Best Case: O(n)\n",
39+
"- Average Case: O(n * n)\n",
40+
"- Worst Case: O(n * n)"
41+
]
42+
},
43+
{
44+
"cell_type": "markdown",
45+
"metadata": {},
46+
"source": [
47+
"## Code for executing and seeing the difference in time complexities"
48+
]
49+
},
50+
{
51+
"cell_type": "markdown",
52+
"metadata": {},
53+
"source": [
54+
"### Best Case Performance:"
55+
]
56+
},
57+
{
58+
"cell_type": "code",
59+
"execution_count": 18,
60+
"metadata": {},
61+
"outputs": [
62+
{
63+
"name": "stdout",
64+
"output_type": "stream",
65+
"text": [
66+
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
67+
"18\n"
68+
]
69+
}
70+
],
71+
"source": [
72+
"# elements are already sorted\n",
73+
"array = [i for i in range(1, 20)]\n",
74+
"\n",
75+
"insertion_sort(array)\n",
76+
"# 20 ALREADY sorted elements need 18 iterations approx = n\n",
77+
"print(array)\n",
78+
"print(iterations)"
79+
]
80+
},
81+
{
82+
"cell_type": "markdown",
83+
"metadata": {},
84+
"source": [
85+
"### Average Case Performance:"
86+
]
87+
},
88+
{
89+
"cell_type": "code",
90+
"execution_count": 19,
91+
"metadata": {},
92+
"outputs": [
93+
{
94+
"name": "stdout",
95+
"output_type": "stream",
96+
"text": [
97+
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
98+
"89\n"
99+
]
100+
}
101+
],
102+
"source": [
103+
"import random\n",
104+
"# elements are randomly shuffled\n",
105+
"array = [i for i in range(1, 20)]\n",
106+
"random.shuffle(array)\n",
107+
"\n",
108+
"insertion_sort(array)\n",
109+
"# 20 shuffled elements need 324 iterations approx = n * n\n",
110+
"print(array)\n",
111+
"print(iterations)"
112+
]
113+
},
114+
{
115+
"cell_type": "markdown",
116+
"metadata": {},
117+
"source": [
118+
"### Worst Case Performance:"
119+
]
120+
},
121+
{
122+
"cell_type": "code",
123+
"execution_count": 20,
124+
"metadata": {},
125+
"outputs": [
126+
{
127+
"name": "stdout",
128+
"output_type": "stream",
129+
"text": [
130+
"[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n",
131+
"171\n"
132+
]
133+
}
134+
],
135+
"source": [
136+
"# elements are reverse sorted\n",
137+
"array = [i for i in range(1, 20)]\n",
138+
"# reversing the array\n",
139+
"array = array[::-1]\n",
140+
"\n",
141+
"insertion_sort(array)\n",
142+
"# 20 REVERSE sorted elements need 324 iterations approx = n * n\n",
143+
"\n",
144+
"print(array)\n",
145+
"print(iterations)"
146+
]
147+
},
148+
{
149+
"cell_type": "markdown",
150+
"metadata": {},
151+
"source": [
152+
"## Applications"
153+
]
154+
},
155+
{
156+
"cell_type": "markdown",
157+
"metadata": {},
158+
"source": [
159+
"More efficient that other sorts when N is comparatively smaller, say < 30."
160+
]
161+
}
162+
],
163+
"metadata": {
164+
"kernelspec": {
165+
"display_name": "Python 3",
166+
"language": "python",
167+
"name": "python3"
168+
},
169+
"language_info": {
170+
"codemirror_mode": {
171+
"name": "ipython",
172+
"version": 3
173+
},
174+
"file_extension": ".py",
175+
"mimetype": "text/x-python",
176+
"name": "python",
177+
"nbconvert_exporter": "python",
178+
"pygments_lexer": "ipython3",
179+
"version": "3.5.2"
180+
}
181+
},
182+
"nbformat": 4,
183+
"nbformat_minor": 2
184+
}

0 commit comments

Comments
 (0)