x的n次方实现插入排序
时间: 2024-06-13 16:06:46 浏览: 126
以下是Python实现x的n次方和插入排序的例子:
1. x的n次方实现
```python
def power(x, n):
if n == 0:
return 1
elif n % 2 == 0:
return power(x*x, n/2)
else:
return x * power(x, n-1)
print(power(2, 3)) # 输出:8
```
2. 插入排序实现
```python
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
arr = [12, 11, 13, 5, 6]
insertion_sort(arr)
print(arr) # 输出:[5, 6, 11, 12, 13]
```
阅读全文