Skip to content

Commit 1cd9f76

Browse files
committed
converted to Insertion
1 parent 1969607 commit 1cd9f76

File tree

2 files changed

+16
-44
lines changed

2 files changed

+16
-44
lines changed

Sorting/SelectionSort-Copy1.ipynb renamed to Sorting/InsertionSort.ipynb

Lines changed: 11 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,45 +2,21 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 4,
5+
"execution_count": null,
66
"metadata": {},
7-
"outputs": [
8-
{
9-
"name": "stdin",
10-
"output_type": "stream",
11-
"text": [
12-
"Enter N 5\n",
13-
" 4\n",
14-
" 3\n",
15-
" 5\n",
16-
" 2\n",
17-
" 3\n"
18-
]
19-
},
20-
{
21-
"name": "stdout",
22-
"output_type": "stream",
23-
"text": [
24-
"Sorted array is:\n",
25-
"4\n",
26-
"3\n",
27-
"5\n",
28-
"2\n",
29-
"3\n"
30-
]
31-
}
32-
],
7+
"outputs": [],
338
"source": [
34-
"def Selection(arr):\n",
9+
"### def Selection(arr):\n",
3510
" n = len(arr)\n",
3611
"\n",
37-
" for i in range(len(arr)): \n",
38-
" min_idx = i \n",
39-
" for j in range(i+1, len(arr)): \n",
40-
" if arr[min_idx] > arr[j]: \n",
41-
" min_idx = j \n",
42-
" arr[i], arr[min_idx] = arr[min_idx], arr[i] \n",
43-
" \n",
12+
" for i in range(1, len(arr)): \n",
13+
" key = arr[i] \n",
14+
" j = i-1\n",
15+
" while j >=0 and key < arr[j] : \n",
16+
" arr[j+1] = arr[j] \n",
17+
" j -= 1\n",
18+
" arr[j+1] = key \n",
19+
" \n",
4420
"\n",
4521
"arr = []\n",
4622
"n=int(input(\"Enter N\"))\n",

Sorting/SelectionSort.ipynb

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@
22
"cells": [
33
{
44
"cell_type": "code",
5-
"execution_count": 4,
5+
"execution_count": 6,
66
"metadata": {},
77
"outputs": [
88
{
99
"name": "stdin",
1010
"output_type": "stream",
1111
"text": [
12-
"Enter N 5\n",
12+
"Enter N 3\n",
1313
" 4\n",
14-
" 3\n",
1514
" 5\n",
16-
" 2\n",
17-
" 3\n"
15+
" 2\n"
1816
]
1917
},
2018
{
2119
"name": "stdout",
2220
"output_type": "stream",
2321
"text": [
2422
"Sorted array is:\n",
25-
"4\n",
26-
"3\n",
27-
"5\n",
2823
"2\n",
29-
"3\n"
24+
"4\n",
25+
"5\n"
3026
]
3127
}
3228
],

0 commit comments

Comments
 (0)