Skip to content

Commit 8225fc9

Browse files
committed
Merge Sort
1 parent 79122ab commit 8225fc9

File tree

2 files changed

+70
-78
lines changed

2 files changed

+70
-78
lines changed

Sorting/InsertionSort-Copy1.ipynb

Lines changed: 0 additions & 78 deletions
This file was deleted.

Sorting/MergeSort.ipynb

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"def merge_sort(alist, start, end):\n",
8+
" '''Sorts the list from indexes start to end - 1 inclusive.'''\n",
9+
" if end - start > 1:\n",
10+
" mid = (start + end)//2\n",
11+
" merge_sort(alist, start, mid)\n",
12+
" merge_sort(alist, mid, end)\n",
13+
" merge_list(alist, start, mid, end)\n",
14+
" \n",
15+
"def merge_list(alist, start, mid, end):\n",
16+
" left = alist[start:mid]\n",
17+
" right = alist[mid:end]\n",
18+
" k = start\n",
19+
" i = 0\n",
20+
" j = 0\n",
21+
" while (start + i < mid and mid + j < end):\n",
22+
" if (left[i] <= right[j]):\n",
23+
" alist[k] = left[i]\n",
24+
" i = i + 1\n",
25+
" else:\n",
26+
" alist[k] = right[j]\n",
27+
" j = j + 1\n",
28+
" k = k + 1\n",
29+
" if start + i < mid:\n",
30+
" while k < end:\n",
31+
" alist[k] = left[i]\n",
32+
" i = i + 1\n",
33+
" k = k + 1\n",
34+
" else:\n",
35+
" while k < end:\n",
36+
" alist[k] = right[j]\n",
37+
" j = j + 1\n",
38+
" k = k + 1\n",
39+
" \n",
40+
" \n",
41+
"alist = input('Enter the list of numbers: ').split()\n",
42+
"alist = [int(x) for x in alist]\n",
43+
"merge_sort(alist, 0, len(alist))\n",
44+
"print('Sorted list: ', end='')\n",
45+
"print(alist)"
46+
]
47+
}
48+
],
49+
"metadata": {
50+
"kernelspec": {
51+
"display_name": "Python 3",
52+
"language": "python",
53+
"name": "python3"
54+
},
55+
"language_info": {
56+
"codemirror_mode": {
57+
"name": "ipython",
58+
"version": 3
59+
},
60+
"file_extension": ".py",
61+
"mimetype": "text/x-python",
62+
"name": "python",
63+
"nbconvert_exporter": "python",
64+
"pygments_lexer": "ipython3",
65+
"version": "3.7.4"
66+
}
67+
},
68+
"nbformat": 4,
69+
"nbformat_minor": 4
70+
}

0 commit comments

Comments
 (0)