希尔排序 快速排序 归并排序 堆排序 计数排序 桶排序 基数排序
时间: 2023-05-26 16:06:26 浏览: 153
希尔排序(Shell Sort):
将一个数组按照一定的增量(例如每隔h个元素进行一次比较和交换)进行排序,从而可以减小数组中的逆序对数量,提高后续的排序效率。
快速排序(Quick Sort):
选择一个基准值(通常选择头部或尾部元素),将数组分成两个部分,一部分小于基准值,一部分大于基准值,对于这两个部分分别进行快速排序,直到分块后的数组大小小于等于1。
归并排序(Merge Sort):
将一个数组按照二分法的思想递归地分成两个数组,直到分块后的数组大小为1,然后不断地将这些分成的小数组进行排序合并,最终得到有序数组。
堆排序(Heap Sort):
使用堆这种数据结构进行排序,将一个数组看成一个大顶堆或小顶堆,不断地和顶端的元素交换,从而得到有序数组。
计数排序(Counting Sort):
将一组数据按照大小进行计数,得到每个元素之前有多少个元素小于它,然后按照这个顺序将这些数据放入一个新的数组中。
桶排序(Bucket Sort):
将一组数据按照大小划分成多个桶,桶内元素按照大小排序,最终合并所有桶的元素得到有序数组。
基数排序(Radix Sort):
将一组数据按照位数进行排序,先按照个位数排序,然后按照十位数排序,以此类推,直到最高位排序完成,得到有序数组。
相关问题
请用C语言分别实现 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归并排序 堆排序 计数排序 桶排序 基数排序
好的,以下是C语言实现各种排序方法的代码示例:
1. 冒泡排序
``` c
void bubble_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
```
2. 选择排序
``` c
void selection_sort(int arr[], int n) {
for (int i = 0; i < n - 1; i++) {
int min_index = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_index]) {
min_index = j;
}
}
int temp = arr[i];
arr[i] = arr[min_index];
arr[min_index] = temp;
}
}
```
3. 插入排序
``` c
void insertion_sort(int arr[], int n) {
for (int i = 1; i < n; i++) {
int key = arr[i];
int j = i - 1;
while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j--;
}
arr[j + 1] = key;
}
}
```
4. 希尔排序
``` c
void shell_sort(int arr[], int n) {
for (int gap = n / 2; gap > 0; gap /= 2) {
for (int i = gap; i < n; i++) {
int temp = arr[i];
int j;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
}
}
```
5. 快速排序
``` c
void quick_sort(int arr[], int left, int right) {
if (left < right) {
int pivot = partition(arr, left, right);
quick_sort(arr, left, pivot - 1);
quick_sort(arr, pivot + 1, right);
}
}
int partition(int arr[], int left, int right) {
int pivot = arr[right];
int i = left - 1;
for (int j = left; j < right; j++) {
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
int temp = arr[i + 1];
arr[i + 1] = arr[right];
arr[right] = temp;
return i + 1;
}
```
6. 归并排序
``` c
void merge_sort(int arr[], int left, int right) {
if (left < right) {
int mid = left + (right - left) / 2;
merge_sort(arr, left, mid);
merge_sort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
void merge(int arr[], int left, int mid, int right) {
int i = left, j = mid + 1, k = 0;
int temp[right - left + 1];
while (i <= mid && j <= right) {
if (arr[i] < arr[j]) {
temp[k++] = arr[i++];
} else {
temp[k++] = arr[j++];
}
}
while (i <= mid) {
temp[k++] = arr[i++];
}
while (j <= right) {
temp[k++] = arr[j++];
}
for (int m = left, n = 0; m <= right; m++, n++) {
arr[m] = temp[n];
}
}
```
7. 堆排序
``` c
void heap_sort(int arr[], int n) {
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
for (int i = n - 1; i > 0; i--) {
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
heapify(arr, i, 0);
}
}
void heapify(int arr[], int n, int i) {
int largest = i;
int left = 2 * i + 1;
int right = 2 * i + 2;
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
if (largest != i) {
int temp = arr[i];
arr[i] = arr[largest];
arr[largest] = temp;
heapify(arr, n, largest);
}
}
```
8. 计数排序
``` c
void counting_sort(int arr[], int n) {
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
int count[max - min + 1];
memset(count, 0, sizeof(count));
for (int i = 0; i < n; i++) {
count[arr[i] - min]++;
}
for (int i = 1; i < max - min + 1; i++) {
count[i] += count[i - 1];
}
int temp[n];
for (int i = n - 1; i >= 0; i--) {
temp[--count[arr[i] - min]] = arr[i];
}
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
```
9. 桶排序
``` c
void bucket_sort(int arr[], int n) {
int max = arr[0], min = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
if (arr[i] < min) {
min = arr[i];
}
}
int bucket_num = (max - min) / n + 1;
int bucket[bucket_num][n];
int bucket_count[bucket_num];
memset(bucket_count, 0, sizeof(bucket_count));
for (int i = 0; i < n; i++) {
int index = (arr[i] - min) / n;
bucket[index][bucket_count[index]] = arr[i];
bucket_count[index]++;
}
int k = 0;
for (int i = 0; i < bucket_num; i++) {
if (bucket_count[i] > 0) {
insertion_sort(bucket[i], bucket_count[i]);
for (int j = 0; j < bucket_count[i]; j++) {
arr[k++] = bucket[i][j];
}
}
}
}
```
10. 基数排序
``` c
void radix_sort(int arr[], int n) {
int max = arr[0];
for (int i = 1; i < n; i++) {
if (arr[i] > max) {
max = arr[i];
}
}
for (int exp = 1; max / exp > 0; exp *= 10) {
counting_sort_by_digit(arr, n, exp);
}
}
void counting_sort_by_digit(int arr[], int n, int exp) {
int count[10];
memset(count, 0, sizeof(count));
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
int temp[n];
for (int i = n - 1; i >= 0; i--) {
temp[--count[(arr[i] / exp) % 10]] = arr[i];
}
for (int i = 0; i < n; i++) {
arr[i] = temp[i];
}
}
```
以上是C语言实现各种排序方法的代码示例,可以根据具体的需求进行选择。
希尔排序 快速排序 归并排序 堆排序 计数排序 桶排序 基数排序代码实现举例
希尔排序:
```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
```
快速排序:
```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)
```
归并排序:
```python
def merge_sort(arr):
if len(arr) <= 1:
return arr
mid = len(arr) // 2
left = merge_sort(arr[:mid])
right = merge_sort(arr[mid:])
return merge(left, right)
def merge(left, right):
result = []
i, j = 0, 0
while i < len(left) and j < len(right):
if left[i] < right[j]:
result.append(left[i])
i += 1
else:
result.append(right[j])
j += 1
result += left[i:]
result += right[j:]
return result
```
堆排序:
```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
```
计数排序:
```python
def counting_sort(arr):
max_value = max(arr)
counts = [0] * (max_value + 1)
result = [0] * len(arr)
for x in arr:
counts[x] += 1
for i in range(1, max_value + 1):
counts[i] += counts[i-1]
for i in range(len(arr)):
result[counts[arr[i]] - 1] = arr[i]
counts[arr[i]] -= 1
return result
```
桶排序:
```python
def bucket_sort(arr):
max_value = max(arr)
bucket_size = max_value // 10
num_buckets = max_value // bucket_size + 1
buckets = [[] for _ in range(num_buckets)]
for x in arr:
buckets[x // bucket_size].append(x)
result = []
for bucket in buckets:
result.extend(sorted(bucket))
return result
```
基数排序:
```python
def radix_sort(arr):
max_value = max(arr)
digit_count = len(str(max_value))
for digit in range(digit_count):
buckets = [[] for _ in range(10)]
for x in arr:
bucket_index = (x // (10 ** digit)) % 10
buckets[bucket_index].append(x)
arr = []
for bucket in buckets:
arr.extend(bucket)
return arr
```
阅读全文