Skip to content

Commit e06f0e9

Browse files
committed
add sorting-algorithms
1 parent 4baf0e2 commit e06f0e9

File tree

1 file changed

+248
-0
lines changed

1 file changed

+248
-0
lines changed
Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,248 @@
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": 14,
13+
"metadata": {},
14+
"outputs": [
15+
{
16+
"name": "stdout",
17+
"output_type": "stream",
18+
"text": [
19+
"[2, 3, 4, 7]\n"
20+
]
21+
}
22+
],
23+
"source": [
24+
"list_ = [4, 3, 2, 7]\n",
25+
"lst_ = []\n",
26+
"\n",
27+
"for i in list_:\n",
28+
" for j in range(len(lst_)):\n",
29+
" if i < lst_[j]:\n",
30+
" lst_.insert(j, i)\n",
31+
" break\n",
32+
" else:\n",
33+
" lst_.append(i)\n",
34+
"\n",
35+
"print(lst_)"
36+
]
37+
},
38+
{
39+
"cell_type": "code",
40+
"execution_count": null,
41+
"metadata": {},
42+
"outputs": [],
43+
"source": []
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": null,
48+
"metadata": {},
49+
"outputs": [],
50+
"source": []
51+
},
52+
{
53+
"cell_type": "markdown",
54+
"metadata": {},
55+
"source": [
56+
"## Merge Sort"
57+
]
58+
},
59+
{
60+
"cell_type": "markdown",
61+
"metadata": {},
62+
"source": [
63+
"#### Merge Algorithms"
64+
]
65+
},
66+
{
67+
"cell_type": "code",
68+
"execution_count": 11,
69+
"metadata": {},
70+
"outputs": [],
71+
"source": [
72+
"def merge(list_1, list_2):\n",
73+
"\n",
74+
" list_3 = []\n",
75+
"\n",
76+
" i = j = 0\n",
77+
"\n",
78+
" len1 = len(list_1)\n",
79+
" len2 = len(list_2)\n",
80+
"\n",
81+
" while i < len1 and j < len2:\n",
82+
"\n",
83+
" if list_1[i] < list_2[j]:\n",
84+
" list_3.append(list_1[i])\n",
85+
" i += 1\n",
86+
"\n",
87+
" else:\n",
88+
" list_3.append(list_2[j])\n",
89+
" j += 1\n",
90+
"\n",
91+
" list_3 += list_1[i:]\n",
92+
" list_3 += list_2[j:]\n",
93+
"\n",
94+
"\n",
95+
" return list_3"
96+
]
97+
},
98+
{
99+
"cell_type": "code",
100+
"execution_count": 12,
101+
"metadata": {},
102+
"outputs": [
103+
{
104+
"data": {
105+
"text/plain": [
106+
"[0, 2, 4, 4, 5, 6, 6, 7, 8, 10, 13]"
107+
]
108+
},
109+
"execution_count": 12,
110+
"metadata": {},
111+
"output_type": "execute_result"
112+
}
113+
],
114+
"source": [
115+
"list_1 = [0, 2, 4, 5, 6]\n",
116+
"list_2 = [4, 6, 7, 8, 10, 13]\n",
117+
"merge(list_1, list_2)"
118+
]
119+
},
120+
{
121+
"cell_type": "markdown",
122+
"metadata": {},
123+
"source": [
124+
"#### Merge Sort Algorithms"
125+
]
126+
},
127+
{
128+
"cell_type": "code",
129+
"execution_count": 13,
130+
"metadata": {},
131+
"outputs": [],
132+
"source": [
133+
"def merge_sort(list_):\n",
134+
" if len(list_) == 1:\n",
135+
" return list_\n",
136+
" else:\n",
137+
" pass"
138+
]
139+
},
140+
{
141+
"cell_type": "code",
142+
"execution_count": null,
143+
"metadata": {},
144+
"outputs": [],
145+
"source": []
146+
},
147+
{
148+
"cell_type": "code",
149+
"execution_count": null,
150+
"metadata": {},
151+
"outputs": [],
152+
"source": []
153+
},
154+
{
155+
"cell_type": "markdown",
156+
"metadata": {},
157+
"source": [
158+
"## Quick Sort"
159+
]
160+
},
161+
{
162+
"cell_type": "code",
163+
"execution_count": 1,
164+
"metadata": {},
165+
"outputs": [],
166+
"source": [
167+
"sample_list = [3, 4, 5, 6, 9, 1]"
168+
]
169+
},
170+
{
171+
"cell_type": "code",
172+
"execution_count": 2,
173+
"metadata": {},
174+
"outputs": [],
175+
"source": [
176+
"def quick_sort(_list, low, high):\n",
177+
"\n",
178+
" pivot = low\n",
179+
" i = low\n",
180+
" j = high\n",
181+
" while i < j:\n",
182+
" while _list[i] <= _list[pivot] and i < high:\n",
183+
" i+= 1\n",
184+
"\n",
185+
" while _list[j] > _list[pivot]:\n",
186+
" j-= 1\n",
187+
"\n",
188+
" if i < j:\n",
189+
" _list[i], _list[j] = _list[j], _list[i]\n",
190+
"\n",
191+
" _list[j], _list[pivot] = _list[pivot], _list[j]\n",
192+
"\n",
193+
" quick_sort(_list, low, j-1)\n",
194+
" quick_sort(_list, j+1, high)\n",
195+
"\n",
196+
" return _list\n"
197+
]
198+
},
199+
{
200+
"cell_type": "code",
201+
"execution_count": 3,
202+
"metadata": {},
203+
"outputs": [
204+
{
205+
"data": {
206+
"text/plain": [
207+
"[1, 3, 4, 5, 6, 9]"
208+
]
209+
},
210+
"execution_count": 3,
211+
"metadata": {},
212+
"output_type": "execute_result"
213+
}
214+
],
215+
"source": [
216+
"quick_sort(sample_list, 0 , len(sample_list)-1)"
217+
]
218+
},
219+
{
220+
"cell_type": "code",
221+
"execution_count": null,
222+
"metadata": {},
223+
"outputs": [],
224+
"source": []
225+
}
226+
],
227+
"metadata": {
228+
"kernelspec": {
229+
"display_name": "ml_env",
230+
"language": "python",
231+
"name": "python3"
232+
},
233+
"language_info": {
234+
"codemirror_mode": {
235+
"name": "ipython",
236+
"version": 3
237+
},
238+
"file_extension": ".py",
239+
"mimetype": "text/x-python",
240+
"name": "python",
241+
"nbconvert_exporter": "python",
242+
"pygments_lexer": "ipython3",
243+
"version": "3.9.18"
244+
}
245+
},
246+
"nbformat": 4,
247+
"nbformat_minor": 2
248+
}

0 commit comments

Comments
 (0)