Skip to content

Commit 6232b98

Browse files
authoredNov 21, 2022
Add files via upload
1 parent b260fca commit 6232b98

File tree

1 file changed

+362
-0
lines changed

1 file changed

+362
-0
lines changed
 

‎Sorting.ipynb

+362
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,362 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"id": "3c566057",
6+
"metadata": {},
7+
"source": [
8+
"# Sorting - List"
9+
]
10+
},
11+
{
12+
"cell_type": "code",
13+
"execution_count": 21,
14+
"id": "442a0a28",
15+
"metadata": {
16+
"ExecuteTime": {
17+
"end_time": "2022-11-18T17:07:55.713777Z",
18+
"start_time": "2022-11-18T17:07:55.701743Z"
19+
}
20+
},
21+
"outputs": [],
22+
"source": [
23+
"li = [9, 1, 8, 2, 7, 3, 6, 4, 5]"
24+
]
25+
},
26+
{
27+
"cell_type": "code",
28+
"execution_count": 22,
29+
"id": "8ff04339",
30+
"metadata": {
31+
"ExecuteTime": {
32+
"end_time": "2022-11-18T17:07:55.964429Z",
33+
"start_time": "2022-11-18T17:07:55.950431Z"
34+
}
35+
},
36+
"outputs": [],
37+
"source": [
38+
"si_li = sorted(li) # Non - inplace sorting"
39+
]
40+
},
41+
{
42+
"cell_type": "code",
43+
"execution_count": 23,
44+
"id": "45d711ab",
45+
"metadata": {
46+
"ExecuteTime": {
47+
"end_time": "2022-11-18T17:07:56.279131Z",
48+
"start_time": "2022-11-18T17:07:56.270140Z"
49+
}
50+
},
51+
"outputs": [
52+
{
53+
"name": "stdout",
54+
"output_type": "stream",
55+
"text": [
56+
"Original List : [9, 1, 8, 2, 7, 3, 6, 4, 5] \n",
57+
"Sorted List : [1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
58+
]
59+
}
60+
],
61+
"source": [
62+
"print( \"Original List : \",li,\"\\nSorted List : \", si_li)"
63+
]
64+
},
65+
{
66+
"cell_type": "code",
67+
"execution_count": 24,
68+
"id": "92efe241",
69+
"metadata": {
70+
"ExecuteTime": {
71+
"end_time": "2022-11-18T17:08:20.529716Z",
72+
"start_time": "2022-11-18T17:08:20.514042Z"
73+
}
74+
},
75+
"outputs": [
76+
{
77+
"name": "stdout",
78+
"output_type": "stream",
79+
"text": [
80+
"Sorting using list.sort method --- Inplace sorting \n",
81+
"Orginal List: [1, 2, 3, 4, 5, 6, 7, 8, 9]\n"
82+
]
83+
}
84+
],
85+
"source": [
86+
"li.sort()\n",
87+
"print(\"Sorting using list.sort method --- Inplace sorting\",\\\n",
88+
" \"\\nOrginal List: \", li)"
89+
]
90+
},
91+
{
92+
"cell_type": "code",
93+
"execution_count": 25,
94+
"id": "d4d10156",
95+
"metadata": {
96+
"ExecuteTime": {
97+
"end_time": "2022-11-18T17:10:38.895040Z",
98+
"start_time": "2022-11-18T17:10:38.883998Z"
99+
}
100+
},
101+
"outputs": [
102+
{
103+
"name": "stdout",
104+
"output_type": "stream",
105+
"text": [
106+
"Sorting using list.sort method --- Inplace sorting \n",
107+
"Orginal List: [9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
108+
]
109+
}
110+
],
111+
"source": [
112+
"li.sort(reverse=True)\n",
113+
"print(\"Sorting using list.sort method --- Inplace sorting\",\\\n",
114+
" \"\\nOrginal List: \", li)"
115+
]
116+
},
117+
{
118+
"cell_type": "code",
119+
"execution_count": 27,
120+
"id": "3640cee4",
121+
"metadata": {
122+
"ExecuteTime": {
123+
"end_time": "2022-11-18T17:11:33.546082Z",
124+
"start_time": "2022-11-18T17:11:33.530429Z"
125+
}
126+
},
127+
"outputs": [
128+
{
129+
"name": "stdout",
130+
"output_type": "stream",
131+
"text": [
132+
"Original List : [9, 1, 8, 2, 7, 3, 6, 4, 5] \n",
133+
"Sorted List : [9, 8, 7, 6, 5, 4, 3, 2, 1]\n"
134+
]
135+
}
136+
],
137+
"source": [
138+
"li = [9, 1, 8, 2, 7, 3, 6, 4, 5]\n",
139+
"si_li = sorted(li, reverse=True)\n",
140+
"print( \"Original List : \",li,\"\\nSorted List : \", si_li)"
141+
]
142+
},
143+
{
144+
"cell_type": "markdown",
145+
"id": "e59e6480",
146+
"metadata": {},
147+
"source": [
148+
"# Sorting - Tuple"
149+
]
150+
},
151+
{
152+
"cell_type": "code",
153+
"execution_count": 30,
154+
"id": "2e6ef92f",
155+
"metadata": {
156+
"ExecuteTime": {
157+
"end_time": "2022-11-18T17:13:27.710500Z",
158+
"start_time": "2022-11-18T17:13:27.700459Z"
159+
}
160+
},
161+
"outputs": [
162+
{
163+
"name": "stdout",
164+
"output_type": "stream",
165+
"text": [
166+
"Original Tuple: (9, 1, 8, 2, 7, 3, 6, 4, 5) \n",
167+
"Sorted Tuple into list: [1, 2, 3, 4, 5, 6, 7, 8, 9] \n",
168+
"Sorted Tuple: (1, 2, 3, 4, 5, 6, 7, 8, 9)\n"
169+
]
170+
}
171+
],
172+
"source": [
173+
"t = (9, 1, 8, 2, 7, 3, 6, 4, 5)\n",
174+
"\n",
175+
"s_li = sorted(t)\n",
176+
"\n",
177+
"print(\"Original Tuple: \", t,\\\n",
178+
" \"\\nSorted Tuple into list: \", s_li,\\\n",
179+
" \"\\nSorted Tuple: \", tuple(s_li))\n",
180+
"\n",
181+
"# There is no In-place sorting for tuple because of its immutability"
182+
]
183+
},
184+
{
185+
"cell_type": "code",
186+
"execution_count": 33,
187+
"id": "943fa2ce",
188+
"metadata": {
189+
"ExecuteTime": {
190+
"end_time": "2022-11-18T17:14:38.947041Z",
191+
"start_time": "2022-11-18T17:14:38.931411Z"
192+
}
193+
},
194+
"outputs": [
195+
{
196+
"data": {
197+
"text/plain": [
198+
"(1, 2, 3, 4, 5, 6, 7, 8, 9)"
199+
]
200+
},
201+
"execution_count": 33,
202+
"metadata": {},
203+
"output_type": "execute_result"
204+
}
205+
],
206+
"source": [
207+
"tuple(sorted((9, 1, 8, 2, 7, 3, 6, 4, 5)))"
208+
]
209+
},
210+
{
211+
"cell_type": "markdown",
212+
"id": "570b7374",
213+
"metadata": {},
214+
"source": [
215+
"# Sorting - Dictionary"
216+
]
217+
},
218+
{
219+
"cell_type": "code",
220+
"execution_count": 35,
221+
"id": "2f1e5d94",
222+
"metadata": {
223+
"ExecuteTime": {
224+
"end_time": "2022-11-18T17:22:34.225699Z",
225+
"start_time": "2022-11-18T17:22:34.213719Z"
226+
}
227+
},
228+
"outputs": [
229+
{
230+
"name": "stdout",
231+
"output_type": "stream",
232+
"text": [
233+
"Original Dict : {'A': 1, 'D': 4, 'B': 2, 'C': 3} \n",
234+
"Sorted dict(keys) : ['A', 'B', 'C', 'D'] \n",
235+
"Sorted dict(values) : [1, 2, 3, 4]\n"
236+
]
237+
}
238+
],
239+
"source": [
240+
"# Sorting on Dictionaries\n",
241+
"d = {'A': 1, 'D': 4, 'B': 2, 'C': 3}\n",
242+
"\n",
243+
"s_d = sorted(d)\n",
244+
"s_d_values = sorted(d.values())\n",
245+
"\n",
246+
"print('Original Dict : ', d,\\\n",
247+
" '\\nSorted dict(keys) : ', s_d,\\\n",
248+
" '\\nSorted dict(values) : ', s_d_values)\n",
249+
" "
250+
]
251+
},
252+
{
253+
"cell_type": "markdown",
254+
"id": "578e1145",
255+
"metadata": {},
256+
"source": [
257+
"# Sorting - Keys"
258+
]
259+
},
260+
{
261+
"cell_type": "code",
262+
"execution_count": 37,
263+
"id": "1a400a6f",
264+
"metadata": {
265+
"ExecuteTime": {
266+
"end_time": "2022-11-18T17:45:37.335981Z",
267+
"start_time": "2022-11-18T17:45:37.325977Z"
268+
}
269+
},
270+
"outputs": [
271+
{
272+
"name": "stdout",
273+
"output_type": "stream",
274+
"text": [
275+
"Original list : [-9, 1, 8, 2, -7, 3, 6, -4, 5] \n",
276+
"Sorted list(abs) : [1, 2, 3, -4, 5, 6, -7, 8, -9]\n"
277+
]
278+
}
279+
],
280+
"source": [
281+
"# Sorting based on keys\n",
282+
"li = [-9, 1, 8, 2, -7, 3, 6, -4, 5]\n",
283+
"si_li = sorted(li, key=abs)\n",
284+
"\n",
285+
"print('Original list : ', li,\\\n",
286+
" '\\nSorted list(abs) : ', si_li)"
287+
]
288+
},
289+
{
290+
"cell_type": "code",
291+
"execution_count": null,
292+
"id": "95d26684",
293+
"metadata": {},
294+
"outputs": [],
295+
"source": []
296+
}
297+
],
298+
"metadata": {
299+
"hide_input": false,
300+
"kernelspec": {
301+
"display_name": "Python 3 (ipykernel)",
302+
"language": "python",
303+
"name": "python3"
304+
},
305+
"language_info": {
306+
"codemirror_mode": {
307+
"name": "ipython",
308+
"version": 3
309+
},
310+
"file_extension": ".py",
311+
"mimetype": "text/x-python",
312+
"name": "python",
313+
"nbconvert_exporter": "python",
314+
"pygments_lexer": "ipython3",
315+
"version": "3.9.12"
316+
},
317+
"toc": {
318+
"base_numbering": 1,
319+
"nav_menu": {},
320+
"number_sections": true,
321+
"sideBar": true,
322+
"skip_h1_title": false,
323+
"title_cell": "Table of Contents",
324+
"title_sidebar": "Contents",
325+
"toc_cell": false,
326+
"toc_position": {},
327+
"toc_section_display": true,
328+
"toc_window_display": false
329+
},
330+
"varInspector": {
331+
"cols": {
332+
"lenName": 16,
333+
"lenType": 16,
334+
"lenVar": 40
335+
},
336+
"kernels_config": {
337+
"python": {
338+
"delete_cmd_postfix": "",
339+
"delete_cmd_prefix": "del ",
340+
"library": "var_list.py",
341+
"varRefreshCmd": "print(var_dic_list())"
342+
},
343+
"r": {
344+
"delete_cmd_postfix": ") ",
345+
"delete_cmd_prefix": "rm(",
346+
"library": "var_list.r",
347+
"varRefreshCmd": "cat(var_dic_list()) "
348+
}
349+
},
350+
"types_to_exclude": [
351+
"module",
352+
"function",
353+
"builtin_function_or_method",
354+
"instance",
355+
"_Feature"
356+
],
357+
"window_display": false
358+
}
359+
},
360+
"nbformat": 4,
361+
"nbformat_minor": 5
362+
}

0 commit comments

Comments
 (0)
Please sign in to comment.