用python实现所有的排序算法
时间: 2024-06-12 20:04:48 浏览: 74
分别使用Java和Python实现希尔排序算法
以下是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
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i = j = k = 0
while i < len(left_half) and j < len(right_half):
if left_half[i] < right_half[j]:
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
while i < len(left_half):
arr[k] = left_half[i]
i += 1
k += 1
while j < len(right_half):
arr[k] = right_half[j]
j += 1
k += 1
return arr
```
6. 快速排序
```python
def quick_sort(arr):
if len(arr) <= 1:
return arr
else:
pivot = arr[0]
left_arr = [x for x in arr[1:] if x < pivot]
right_arr = [x for x in arr[1:] if x >= pivot]
return quick_sort(left_arr) + [pivot] + quick_sort(right_arr)
```
7. 堆排序
```python
def heapify(arr, n, i):
largest = i
left = 2 * i + 1
right = 2 * i + 2
if left < n and arr[i] < arr[left]:
largest = left
if right < n and arr[largest] < arr[right]:
largest = right
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, bucket_size=5):
if len(arr) == 0:
return arr
min_val, max_val = min(arr), max(arr)
bucket_count = (max_val - min_val) // bucket_size + 1
buckets = [[] for _ in range(bucket_count)]
for i in range(len(arr)):
buckets[(arr[i] - min_val) // bucket_size].append(arr[i])
arr = []
for i in range(len(buckets)):
insertion_sort(buckets[i])
arr += buckets[i]
return arr
```
10. 基数排序
```python
def radix_sort(arr):
max_val = max(arr)
exp = 1
while max_val // exp > 0:
bucket = [[] for _ in range(10)]
for i in arr:
bucket[(i // exp) % 10].append(i)
arr = [i for b in bucket for i in b]
exp *= 10
return arr
```
阅读全文