十大算法python
时间: 2023-11-08 18:06:01 浏览: 142
十个必知的排序算法|Python实例系列[1]
十大算法包括:冒泡排序、选择排序、插入排序、希尔排序、归并排序、快速排序、堆排序、计数排序、桶排序和基数排序。
以下是这些算法的Python实现:
1. 冒泡排序
```python
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
```
2. 选择排序
```python
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_idx = i
for j in range(i+1, n):
if arr[j] < arr[min_idx]:
min_idx = j
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
```
3. 插入排序
```python
def insertion_sort(arr):
n = len(arr)
for i in range(1, n):
key = arr[i]
j = i-1
while j >= 0 and key < arr[j]:
arr[j+1] = arr[j]
j -= 1
arr[j+1] = key
return arr
```
4. 希尔排序
```python
def shell_sort(arr):
n = len(arr)
gap = n // 2
while gap > 0:
for i in range(gap, n):
temp = arr[i]
j = i
while j >= gap and arr[j-gap] > temp:
arr[j] = arr[j-gap]
j -= gap
arr[j] = temp
gap //= 2
return arr
```
5. 归并排序
```python
def merge_sort(arr):
if len(arr) > 1:
mid = len(arr) // 2
L = arr[:mid]
R = arr[mid:]
merge_sort(L)
merge_sort(R)
i = j = k = 0
while i < len(L) and j < len(R):
if L[i] < R[j]:
arr[k] = L[i]
i += 1
else:
arr[k] = R[j]
j += 1
k += 1
while i < len(L):
arr[k] = L[i]
i += 1
k += 1
while j < len(R):
arr[k] = R[j]
j += 1
k += 1
return arr
```
6. 快速排序
```python
def quick_sort(arr):
if len(arr) <= 1:
return arr
pivot = arr[len(arr)//2]
left = [x for x in arr if x < pivot]
middle = [x for x in arr if x == pivot]
right = [x for x in arr if x > pivot]
return quick_sort(left) + middle + quick_sort(right)
```
7. 堆排序
```python
def heapify(arr, n, i):
largest = i
l = 2 * i + 1
r = 2 * i + 2
if l < n and arr[i] < arr[l]:
largest = l
if r < n and arr[largest] < arr[r]:
largest = r
if largest != i:
arr[i], arr[largest] = arr[largest], arr[i]
heapify(arr, n, largest)
def heap_sort(arr):
n = len(arr)
for i in range(n//2-1, -1, -1):
heapify(arr, n, i)
for i in range(n-1, 0, -1):
arr[i], arr[0] = arr[0], arr[i]
heapify(arr, i, 0)
return arr
```
8. 计数排序
```python
def counting_sort(arr):
n = len(arr)
output = [0] * n
count = [0] * (max(arr)+1)
for i in range(n):
count[arr[i]] += 1
for i in range(1, len(count)):
count[i] += count[i-1]
for i in range(n-1, -1, -1):
output[count[arr[i]]-1] = arr[i]
count[arr[i]] -= 1
for i in range(n):
arr[i] = output[i]
return arr
```
9. 桶排序
```python
def bucket_sort(arr):
n = len(arr)
max_val = max(arr)
bucket_size = max_val // n + 1
buckets = [[] for _ in range(bucket_size)]
for i in range(n):
buckets[arr[i] // bucket_size].append(arr[i])
for i in range(bucket_size):
buckets[i].sort()
k = 0
for i in range(bucket_size):
for j in range(len(buckets[i])):
arr[k] = buckets[i][j]
k += 1
return arr
```
10. 基数排序
```python
def radix_sort(arr):
max_val = max(arr)
exp = 1
while max_val // exp > 0:
counting_sort(arr, exp)
exp *= 10
return arr
def counting_sort(arr, exp):
n = len(arr)
output = [0] * n
count = [0] * 10
for i in range(n):
index = arr[i] // exp
count[index % 10] += 1
for i in range(1, len(count)):
count[i] += count[i-1]
for i in range(n-1, -1, -1):
index = arr[i] // exp
output[count[index % 10]-1] = arr[i]
count[index % 10] -= 1
for i in range(n):
arr[i] = output[i]
```
阅读全文