Skip to content

Commit 71f04d7

Browse files
committed
upload 3,4
1 parent fe0a3a9 commit 71f04d7

File tree

3 files changed

+714
-1
lines changed

3 files changed

+714
-1
lines changed

Diff for: 第一章代码.ipynb

+42-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,47 @@
77
"# 第一章代码"
88
]
99
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## 算法1.1"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 3,
20+
"metadata": {},
21+
"outputs": [
22+
{
23+
"name": "stdout",
24+
"output_type": "stream",
25+
"text": [
26+
"[11, 12, 22, 25, 25, 64]\n"
27+
]
28+
}
29+
],
30+
"source": [
31+
"# author: Zeqi Zhu ([email protected])\n",
32+
"# 选择排序算法\n",
33+
"def selection_sort(A):\n",
34+
" n = len(A)\n",
35+
" for i in range(n - 1):\n",
36+
" # Find the minimum element in the remaining unsorted array\n",
37+
" cur_min_pos = i\n",
38+
" for j in range(i + 1, n):\n",
39+
" if A[j] < A[cur_min_pos]:\n",
40+
" cur_min_pos = j\n",
41+
" # Swap the found minimum element with the first element\n",
42+
" A[i], A[cur_min_pos] = A[cur_min_pos], A[i]\n",
43+
" return A\n",
44+
"\n",
45+
"# Example usage:\n",
46+
"example_array = [64, 25, 25, 12, 22, 11]\n",
47+
"sorted_array = selection_sort(example_array)\n",
48+
"print(sorted_array)"
49+
]
50+
},
1051
{
1152
"cell_type": "markdown",
1253
"metadata": {},
@@ -28,7 +69,7 @@
2869
}
2970
],
3071
"source": [
31-
"# author: ZhuZeqi ([email protected])\n",
72+
"# author: Zeqi Zhu ([email protected])\n",
3273
"# 插入排序算法\n",
3374
"\n",
3475
"def insertion_sort(A):\n",

Diff for: 第三章代码.ipynb

+249
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"# 第三章代码"
8+
]
9+
},
10+
{
11+
"cell_type": "markdown",
12+
"metadata": {},
13+
"source": [
14+
"## 算法3.1: MergeSort"
15+
]
16+
},
17+
{
18+
"cell_type": "code",
19+
"execution_count": 11,
20+
"metadata": {},
21+
"outputs": [],
22+
"source": [
23+
"# author: Zeqi Zhu ([email protected])\n",
24+
"\n",
25+
"def merge_sort(A, l, r):\n",
26+
" if l < r:\n",
27+
" # Same as (l+r)//2, but avoids overflow for large l and r\n",
28+
" mid = l + (r - l) // 2\n",
29+
"\n",
30+
" # Sort first and second halves\n",
31+
" merge_sort(A, l, mid)\n",
32+
" merge_sort(A, mid + 1, r)\n",
33+
" merge(A, l, mid, r)\n",
34+
"\n",
35+
" return A"
36+
]
37+
},
38+
{
39+
"cell_type": "markdown",
40+
"metadata": {},
41+
"source": [
42+
"## 算法3.2: Merge "
43+
]
44+
},
45+
{
46+
"cell_type": "code",
47+
"execution_count": 10,
48+
"metadata": {},
49+
"outputs": [
50+
{
51+
"name": "stdout",
52+
"output_type": "stream",
53+
"text": [
54+
"[5, 6, 7, 11, 12, 12, 13]\n"
55+
]
56+
}
57+
],
58+
"source": [
59+
"def merge(arr, l, m, r):\n",
60+
" n1 = m - l + 1\n",
61+
" n2 = r - m\n",
62+
" B = [0] * (n1 + n2) # Temporary array B\n",
63+
"\n",
64+
" i, j, k = l, m + 1, 0 # Initial indexes\n",
65+
"\n",
66+
" # Merge the subarrays into B\n",
67+
" while i <= m and j <= r:\n",
68+
" if arr[i] <= arr[j]:\n",
69+
" B[k] = arr[i]\n",
70+
" i += 1\n",
71+
" else:\n",
72+
" B[k] = arr[j]\n",
73+
" j += 1\n",
74+
" k += 1\n",
75+
"\n",
76+
" # Copy the remaining elements of left subarray, if there are any\n",
77+
" while i <= m:\n",
78+
" B[k] = arr[i]\n",
79+
" i += 1\n",
80+
" k += 1\n",
81+
"\n",
82+
" # Copy the remaining elements of right subarray, if there are any\n",
83+
" while j <= r:\n",
84+
" B[k] = arr[j]\n",
85+
" j += 1\n",
86+
" k += 1\n",
87+
"\n",
88+
" # Copy the merged elements back into original array arr\n",
89+
" for i in range(l, r + 1):\n",
90+
" arr[i] = B[i - l]\n",
91+
"\n",
92+
" return arr\n",
93+
"\n",
94+
"# Example usage:\n",
95+
"example_array = [12, 12, 11, 13, 5, 6, 7]\n",
96+
"sorted_array = merge_sort(example_array, 0, len(example_array) - 1)\n",
97+
"print(sorted_array) # Output: [5, 6, 7, 11, 12, 12, 13]"
98+
]
99+
},
100+
{
101+
"cell_type": "markdown",
102+
"metadata": {},
103+
"source": [
104+
"## 算法3.3: Inversion-BruteForce"
105+
]
106+
},
107+
{
108+
"cell_type": "code",
109+
"execution_count": 3,
110+
"metadata": {},
111+
"outputs": [
112+
{
113+
"name": "stdout",
114+
"output_type": "stream",
115+
"text": [
116+
"3\n"
117+
]
118+
}
119+
],
120+
"source": [
121+
"def count_inversions_bruteforce(arr):\n",
122+
" n = len(arr)\n",
123+
" sum = 0\n",
124+
" for i in range(n):\n",
125+
" for j in range(i + 1, n):\n",
126+
" if arr[i] > arr[j]:\n",
127+
" sum += 1\n",
128+
" return sum\n",
129+
"\n",
130+
"# Example usage:\n",
131+
"example_array = [2, 4, 1, 3, 5]\n",
132+
"print(count_inversions_bruteforce(example_array)) # Output: 3"
133+
]
134+
},
135+
{
136+
"cell_type": "markdown",
137+
"metadata": {},
138+
"source": [
139+
"## 算法3.4: MergeCount "
140+
]
141+
},
142+
{
143+
"cell_type": "code",
144+
"execution_count": 4,
145+
"metadata": {},
146+
"outputs": [],
147+
"source": [
148+
"def merge_count(arr, l, mid, r):\n",
149+
" i = l\n",
150+
" j = mid + 1\n",
151+
" k = 0\n",
152+
" inv_count = 0 # This will hold the count of inversions\n",
153+
" B = [0] * (r - l + 1) # Temporary array B\n",
154+
" \n",
155+
" # Merge the subarrays and count inversions\n",
156+
" while i <= mid and j <= r:\n",
157+
" if arr[i] > arr[j]:\n",
158+
" B[k] = arr[j]\n",
159+
" # The number of inversions is the number of elements remaining in the left subarray\n",
160+
" inv_count += (mid - i + 1)\n",
161+
" k += 1\n",
162+
" j += 1\n",
163+
" else:\n",
164+
" B[k] = arr[i]\n",
165+
" k += 1\n",
166+
" i += 1\n",
167+
" \n",
168+
" # Copy the remaining elements of left subarray, if there are any\n",
169+
" while i <= mid:\n",
170+
" B[k] = arr[i]\n",
171+
" i += 1\n",
172+
" k += 1\n",
173+
" \n",
174+
" # Copy the remaining elements of right subarray, if there are any\n",
175+
" while j <= r:\n",
176+
" B[k] = arr[j]\n",
177+
" j += 1\n",
178+
" k += 1\n",
179+
" \n",
180+
" # Copy the merged elements back into original array arr\n",
181+
" for i in range(0, k):\n",
182+
" arr[l + i] = B[i]\n",
183+
" \n",
184+
" return inv_count\n"
185+
]
186+
},
187+
{
188+
"cell_type": "markdown",
189+
"metadata": {},
190+
"source": [
191+
"## 算法3.5: CountInver"
192+
]
193+
},
194+
{
195+
"cell_type": "code",
196+
"execution_count": 6,
197+
"metadata": {},
198+
"outputs": [
199+
{
200+
"name": "stdout",
201+
"output_type": "stream",
202+
"text": [
203+
"(45, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\n"
204+
]
205+
}
206+
],
207+
"source": [
208+
"def count_inver(arr, l, r):\n",
209+
" if l >= r:\n",
210+
" return 0, arr[l:r+1]\n",
211+
" \n",
212+
" mid = (l + r) // 2\n",
213+
" s1, left_half = count_inver(arr, l, mid)\n",
214+
" s2, right_half = count_inver(arr, mid + 1, r)\n",
215+
" s3 = merge_count(arr, l, mid, r) # merge_count function from previous implementation\n",
216+
" total = s1 + s2 + s3\n",
217+
" \n",
218+
" return total, arr[l:r+1]\n",
219+
"\n",
220+
"\n",
221+
"# Example usage:\n",
222+
"example_array = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]\n",
223+
"print(count_inver(example_array, 0, len(example_array) - 1)) # Output: 45\n",
224+
"\n"
225+
]
226+
}
227+
],
228+
"metadata": {
229+
"kernelspec": {
230+
"display_name": "py38_torch",
231+
"language": "python",
232+
"name": "python3"
233+
},
234+
"language_info": {
235+
"codemirror_mode": {
236+
"name": "ipython",
237+
"version": 3
238+
},
239+
"file_extension": ".py",
240+
"mimetype": "text/x-python",
241+
"name": "python",
242+
"nbconvert_exporter": "python",
243+
"pygments_lexer": "ipython3",
244+
"version": "3.8.18"
245+
}
246+
},
247+
"nbformat": 4,
248+
"nbformat_minor": 2
249+
}

0 commit comments

Comments
 (0)