@@ -20,7 +20,9 @@ module.exports = () => ({
20
20
language : "py" ,
21
21
text : `
22
22
# 1. 归并排序(推荐!其他排序方法都不推荐在竞赛中使用)
23
+ # 1. Merge sort (recommended! None of the other ranking methods are recommended for use in contests)
23
24
# 归并排序乞丐版
25
+ # Merge sort beggar version
24
26
class Solution:
25
27
def sortArray(self, nums: List[int]) -> List[int]:
26
28
def mergeSort(l, r):
@@ -49,6 +51,7 @@ class Solution:
49
51
mergeSort(0, len(nums) - 1)
50
52
return nums
51
53
# 归并排序优化版
54
+ # Merge sort optimization version
52
55
class Solution:
53
56
def sortArray(self, nums: List[int]) -> List[int]:
54
57
temp = [0] * len(nums)
@@ -83,7 +86,9 @@ class Solution:
83
86
return nums
84
87
85
88
# 2. 快速排序
89
+ # 2. Quick sort
86
90
# 快速排序乞丐版
91
+ # Quick sort beggar version
87
92
class Solution:
88
93
def sortArray(self, nums: List[int]) -> List[int]:
89
94
temp = [0] * len(nums)
@@ -98,6 +103,7 @@ class Solution:
98
103
99
104
return quickSort(nums)
100
105
# 快速排序优化版
106
+ # Quick sort optimization version
101
107
class Solution:
102
108
def sortArray(self, nums: List[int]) -> List[int]:
103
109
temp = [0] * len(nums)
@@ -126,6 +132,7 @@ class Solution:
126
132
return nums
127
133
128
134
# 3. 插入排序
135
+ # 3. Insertion sort
129
136
class Solution:
130
137
def sortArray(self, nums: List[int]) -> List[int]:
131
138
n = len(nums)
@@ -139,6 +146,7 @@ class Solution:
139
146
return nums
140
147
141
148
# 4. 选择排序
149
+ # 4. Selection sort
142
150
class Solution:
143
151
def sortArray(self, nums: List[int]) -> List[int]:
144
152
n = len(nums)
@@ -151,6 +159,7 @@ class Solution:
151
159
return nums
152
160
153
161
# 5. 冒泡排序
162
+ # 5. Bubble sort
154
163
class Solution:
155
164
def sortArray(self, nums: List[int]) -> List[int]:
156
165
n = len(nums)
@@ -179,6 +188,7 @@ class Solution:
179
188
language : "py" ,
180
189
text : `
181
190
# 1. 归并排序(推荐!其他排序方法都不推荐在竞赛中使用)
191
+ # 1. Merge sort (recommended! None of the other ranking methods are recommended for use in contests)
182
192
class Solution:
183
193
def sortList(self, head: ListNode) -> ListNode:
184
194
def mergeSort(head: ListNode) -> ListNode:
@@ -213,9 +223,11 @@ class Solution:
213
223
214
224
return mergeSort(head)
215
225
# 2. 快速排序
226
+ # 2. Quick sort
216
227
class Solution:
217
228
def sortList(self, head):
218
229
# ��坏情况也是 n ^ 2 ,因此面试或者竞赛不建议使用
230
+ # The worst case is also n ^ 2, so it is not recommended to use it in interviews or competitions
219
231
def quickSort(head, end):
220
232
221
233
if head != end:
@@ -226,12 +238,15 @@ class Solution:
226
238
def partition(head, end):
227
239
# p1是写指针,p2是读指针
228
240
# 最终 p1 是大的链表的头, head 是小的链表的头
241
+ # p1 is the write pointer, p2 is the read pointer
242
+ # Finally, p1 is the head of the larger linked list, and head is the head of the smaller linked list
229
243
pivot_val = head.val
230
244
p1, p2 = head, head.next
231
245
232
246
while p2 != end:
233
247
if p2.val < pivot_val:
234
248
# 相当于数组的 append 方法
249
+ # Equivalent to the append method of an array
235
250
p1 = p1.next
236
251
p1.val, p2.val = p2.val, p1.val
237
252
p2 = p2.next
@@ -241,6 +256,7 @@ class Solution:
241
256
quickSort(head, None)
242
257
return head
243
258
# 3. 插入排序
259
+ # 3. Insertion sort
244
260
class Solution:
245
261
def sortList(self, head):
246
262
if head == None or head.next == None:
@@ -252,21 +268,26 @@ class Solution:
252
268
cur = head
253
269
while cur:
254
270
# 准备将 last 插入到合适位置
271
+ # Prepare to insert last into the appropriate position
255
272
last = cur.next
256
273
if last and last.val < cur.val:
257
274
# 从 dummy 到 cur 线性遍历找第一个满足条件的位置并插入
275
+ # Linearly traverse from dummy to cur to find the first position that meets the conditions and insert
258
276
while pre.next and pre.next.val < last.val:
259
277
pre = pre.next
260
278
tmp = pre.next
261
279
pre.next = last
262
- cur.next = last.next # 别忘了这个,否则成环
280
+ # 别忘了这个,否则成环
281
+ # Don't forget this, otherwise it will become a ring
282
+ cur.next = last.next
263
283
last.next = tmp
264
284
pre = dummy
265
285
else:
266
286
cur = last
267
287
268
288
return dummy.next
269
289
# 4. 选择排序
290
+ # 4. Selection sort
270
291
class Solution:
271
292
def sortList(self, head):
272
293
temp = head
@@ -282,6 +303,7 @@ class Solution:
282
303
temp = temp.next
283
304
return head
284
305
# 5. 冒泡排序
306
+ # 5. Bubble sort
285
307
class Solution:
286
308
def sortList(self, head):
287
309
if not head:
0 commit comments