Skip to content

Commit 8dcceb9

Browse files
authoredJul 21, 2022
feat: add csharp solutions to lcof problems (doocs#826)
* 《剑指 Offer(第 2 版)》添加 C# 题解
1 parent c793e10 commit 8dcceb9

File tree

142 files changed

+3515
-5
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+3515
-5
lines changed
 

‎basic/sorting/BubbleSort/README.md

+31
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,37 @@ public class Program
238238
}
239239
```
240240

241+
### **Python3**
242+
243+
```python
244+
def bubbleSort(arr):
245+
n = len(arr)
246+
# Iterate over all array elements
247+
for i in range(n):
248+
# Last i elements are already in place
249+
for j in range(n - i - 1):
250+
if arr[j] > arr[j + 1]:
251+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
252+
253+
254+
# 改进版本
255+
def bubbleSort(arr):
256+
n = len(arr)
257+
for i in range(n - 1):
258+
has_change = False
259+
for j in range(n - i - 1):
260+
if arr[j] > arr[j + 1]:
261+
arr[j], arr[j + 1] = arr[j + 1], arr[j]
262+
has_change = True
263+
if not has_change:
264+
break
265+
266+
267+
arr = [64, 34, 25, 12, 22, 11, 90]
268+
bubbleSort(arr)
269+
print(arr)
270+
```
271+
241272
<!-- tabs:end -->
242273

243274
## 算法分析

‎basic/sorting/InsertionSort/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,22 @@ public class Program
197197
}
198198
```
199199

200+
### **Python3**
201+
202+
```python
203+
def insertion_sort(array):
204+
for i in range(len(array)):
205+
cur_index = i
206+
while array[cur_index - 1] > array[cur_index] and cur_index - 1 >= 0:
207+
array[cur_index], array[cur_index - 1] = array[cur_index - 1], array[cur_index]
208+
cur_index -= 1
209+
return array
210+
211+
array = [10, 17, 50, 7, 30, 24, 27, 45, 15, 5, 36, 21]
212+
print(insertion_sort(array))
213+
214+
```
215+
200216
<!-- tabs:end -->
201217

202218
## 算法分析

0 commit comments

Comments
 (0)