Skip to content

Commit 0eb9ede

Browse files
committed
Added Selection Sort
1 parent e3713eb commit 0eb9ede

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

Sorting/2. Selection_Sort.ipynb

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

0 commit comments

Comments
 (0)