请用C语言分别实现 冒泡排序 选择排序 插入排序 希尔排序 快速排序 归并排序 堆排序 计数排序 桶排序 基数排序
时间: 2023-07-06 18:37:50 浏览: 256
好的,以下是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语言实现各种排序方法的代码示例,可以根据具体的需求进行选择。
阅读全文