Skip to content

Commit b983a21

Browse files
author
snailkn
committed
create
1 parent 66f74bb commit b983a21

File tree

6 files changed

+968
-38
lines changed

6 files changed

+968
-38
lines changed

cha2/cha2_1.ipynb

Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "heading",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"level": 1,
9+
"source": [
10+
"算法基础 - 插入排序"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"> 1.插入法降序排序"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 1,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"import numpy as np\n",
27+
"import random\n",
28+
"import time"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 6,
34+
"metadata": {
35+
"collapsed": true
36+
},
37+
"outputs": [
38+
{
39+
"name": "stdout",
40+
"output_type": "stream",
41+
"text": [
42+
"[26247 27748 31404 ..., 16477 412 25203]\n"
43+
]
44+
},
45+
{
46+
"name": "stdout",
47+
"output_type": "stream",
48+
"text": [
49+
"[49997 49993 49991 ..., 12 9 2]\n15.146586179733276\n"
50+
]
51+
}
52+
],
53+
"source": [
54+
"def insertion_sort(array):\n",
55+
" length = len(array)\n",
56+
" for i in range(1, length):\n",
57+
" key = array[i]\n",
58+
" j = i-1\n",
59+
" while j >= 0 and array[j] < key:\n",
60+
" array[j+1] = array[j]\n",
61+
" j -= 1\n",
62+
" array[j+1] = key\n",
63+
" return array\n",
64+
"\n",
65+
"a = np.array(random.sample(range(50000), 10000))\n",
66+
"print(a)\n",
67+
"t = time.time()\n",
68+
"print(insertion_sort(a))\n",
69+
"print(time.time()-t)"
70+
]
71+
},
72+
{
73+
"cell_type": "markdown",
74+
"metadata": {},
75+
"source": [
76+
"> 2.使用线性查找确认给定数字是否在给定数组中,返回对应的数组下标或者None"
77+
]
78+
},
79+
{
80+
"cell_type": "code",
81+
"execution_count": 3,
82+
"metadata": {},
83+
"outputs": [
84+
{
85+
"name": "stdout",
86+
"output_type": "stream",
87+
"text": [
88+
"[0 1 2 3 4 5 6 7 8 9]\n[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, None]\n"
89+
]
90+
}
91+
],
92+
"source": [
93+
"def find_num(array, num):\n",
94+
" length = len(array)\n",
95+
" i = 0\n",
96+
" while i < length and array[i] != num:\n",
97+
" i += 1\n",
98+
" return i if i < length else None\n",
99+
"\n",
100+
"a = np.array(range(10))\n",
101+
"print(a)\n",
102+
"print([find_num(a, x) for x in range(11)])"
103+
]
104+
},
105+
{
106+
"cell_type": "markdown",
107+
"metadata": {},
108+
"source": [
109+
"> 3.用n元数组模拟两个n位二进制数相加的过程"
110+
]
111+
},
112+
{
113+
"cell_type": "code",
114+
"execution_count": 4,
115+
"metadata": {},
116+
"outputs": [
117+
{
118+
"name": "stdout",
119+
"output_type": "stream",
120+
"text": [
121+
"[0 0 1 1]\n"
122+
]
123+
}
124+
],
125+
"source": [
126+
"def badd(a, b):\n",
127+
" if len(a) != len(b) or (set(a + b) != {0, 1} and set(a + b) != {1}):\n",
128+
" print('input error')\n",
129+
" n = len(a)\n",
130+
" i = temp = 0\n",
131+
" c = np.array([0] * (n + 1))\n",
132+
" while i < n:\n",
133+
" sum = a[i] + b[i] + temp\n",
134+
" c[i], temp = sum % 2, sum // 2\n",
135+
" i += 1\n",
136+
" c[n] = temp\n",
137+
" return c\n",
138+
"print(badd([1, 1, 1], [1, 0, 1]))\n"
139+
]
140+
},
141+
{
142+
"cell_type": "code",
143+
"execution_count": null,
144+
"metadata": {},
145+
"outputs": [],
146+
"source": []
147+
}
148+
],
149+
"metadata": {
150+
"kernelspec": {
151+
"display_name": "Python 2",
152+
"language": "python",
153+
"name": "python2"
154+
},
155+
"language_info": {
156+
"codemirror_mode": {
157+
"name": "ipython",
158+
"version": 2
159+
},
160+
"file_extension": ".py",
161+
"mimetype": "text/x-python",
162+
"name": "python",
163+
"nbconvert_exporter": "python",
164+
"pygments_lexer": "ipython2",
165+
"version": "2.7.6"
166+
}
167+
},
168+
"nbformat": 4,
169+
"nbformat_minor": 0
170+
}

cha2/cha2_2.ipynb

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "heading",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"level": 1,
9+
"source": [
10+
"算法基础 - 分析算法"
11+
]
12+
},
13+
{
14+
"cell_type": "code",
15+
"execution_count": 2,
16+
"metadata": {},
17+
"outputs": [],
18+
"source": [
19+
"import numpy as np\n",
20+
"import random\n",
21+
"import time"
22+
]
23+
},
24+
{
25+
"cell_type": "markdown",
26+
"metadata": {},
27+
"source": [
28+
"> 2.2选择算法对数组进行排序"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 4,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"name": "stdout",
38+
"output_type": "stream",
39+
"text": [
40+
"[13587 28547 10141 ..., 31846 32712 7809]\n"
41+
]
42+
},
43+
{
44+
"name": "stdout",
45+
"output_type": "stream",
46+
"text": [
47+
"[ 0 1 19 ..., 49990 49992 49997]\n10.699007987976074\n"
48+
]
49+
}
50+
],
51+
"source": [
52+
"def selection_sort(array):\n",
53+
" length = len(array)\n",
54+
" m, min = 0, array[0]\n",
55+
" for i in range(1, length):\n",
56+
" for j in range(i, length):\n",
57+
" if array[j] < min:\n",
58+
" m, min = j, array[j]\n",
59+
" j += 1\n",
60+
" array[m] = array[i-1]\n",
61+
" array[i-1] = min\n",
62+
" m, min = i, array[i]\n",
63+
" i += 1\n",
64+
" return array\n",
65+
"\n",
66+
"a = np.array(random.sample(range(50000), 10000)) \n",
67+
"print(a)\n",
68+
"t = time.time()\n",
69+
"print(selection_sort(a))\n",
70+
"print(time.time()-t)"
71+
]
72+
},
73+
{
74+
"cell_type": "code",
75+
"execution_count": null,
76+
"metadata": {},
77+
"outputs": [],
78+
"source": []
79+
}
80+
],
81+
"metadata": {
82+
"kernelspec": {
83+
"display_name": "Python 2",
84+
"language": "python",
85+
"name": "python2"
86+
},
87+
"language_info": {
88+
"codemirror_mode": {
89+
"name": "ipython",
90+
"version": 2
91+
},
92+
"file_extension": ".py",
93+
"mimetype": "text/x-python",
94+
"name": "python",
95+
"nbconvert_exporter": "python",
96+
"pygments_lexer": "ipython2",
97+
"version": "2.7.6"
98+
}
99+
},
100+
"nbformat": 4,
101+
"nbformat_minor": 0
102+
}

cha2/cha2_3.ipynb

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "heading",
5+
"metadata": {
6+
"collapsed": true
7+
},
8+
"level": 1,
9+
"source": [
10+
"算法基础 - 设计算法"
11+
]
12+
},
13+
{
14+
"cell_type": "markdown",
15+
"metadata": {},
16+
"source": [
17+
"> 归并排序"
18+
]
19+
},
20+
{
21+
"cell_type": "code",
22+
"execution_count": 1,
23+
"metadata": {},
24+
"outputs": [],
25+
"source": [
26+
"import numpy as np\n",
27+
"import random\n",
28+
"import time"
29+
]
30+
},
31+
{
32+
"cell_type": "code",
33+
"execution_count": 6,
34+
"metadata": {},
35+
"outputs": [
36+
{
37+
"name": "stdout",
38+
"output_type": "stream",
39+
"text": [
40+
"[ 3875 45858 22 ..., 19760 20885 36795]\n[ 3.00000000e+00 1.50000000e+01 1.60000000e+01 ..., 4.99860000e+04\n 4.99950000e+04 4.99980000e+04]\n0.14264893531799316\n"
41+
]
42+
}
43+
],
44+
"source": [
45+
"def merge(a1, a2):\n",
46+
" l1, l2 = len(a1), len(a2)\n",
47+
" l = l1 + l2\n",
48+
" ar = np.empty(l)\n",
49+
" i = j = r = 0\n",
50+
" while i != l1 and j != l2:\n",
51+
" if a1[i] <= a2[j]:\n",
52+
" ar[r] = a1[i]\n",
53+
" i += 1\n",
54+
" else:\n",
55+
" ar[r] = a2[j]\n",
56+
" j += 1\n",
57+
" r += 1\n",
58+
" if i == l1:\n",
59+
" ar[r:] = a2[j:]\n",
60+
" else:\n",
61+
" ar[r:] = a1[i:]\n",
62+
" return ar\n",
63+
" \n",
64+
"def merge_sort(array):\n",
65+
" length = len(array)\n",
66+
" if length == 1:\n",
67+
" return array\n",
68+
" p = length//2\n",
69+
" a1 = merge_sort(array[0: p])\n",
70+
" a2 = merge_sort(array[p: length])\n",
71+
" return merge(a1, a2)\n",
72+
"\n",
73+
"a = np.array(random.sample(range(50000), 10000))\n",
74+
"print(a)\n",
75+
"t = time.time()\n",
76+
"print(merge_sort(a))\n",
77+
"print(time.time()-t)"
78+
]
79+
},
80+
{
81+
"cell_type": "code",
82+
"execution_count": null,
83+
"metadata": {},
84+
"outputs": [],
85+
"source": []
86+
}
87+
],
88+
"metadata": {
89+
"kernelspec": {
90+
"display_name": "Python 2",
91+
"language": "python",
92+
"name": "python2"
93+
},
94+
"language_info": {
95+
"codemirror_mode": {
96+
"name": "ipython",
97+
"version": 2
98+
},
99+
"file_extension": ".py",
100+
"mimetype": "text/x-python",
101+
"name": "python",
102+
"nbconvert_exporter": "python",
103+
"pygments_lexer": "ipython2",
104+
"version": "2.7.6"
105+
}
106+
},
107+
"nbformat": 4,
108+
"nbformat_minor": 0
109+
}

0 commit comments

Comments
 (0)