随机输入(或随机产生)30个数 用c语言冒泡排序完成对这30个数的排序 (2)分别采用插入、快速和希尔完成对这30个数的排序任务,并输出每一趟排序后的结果
时间: 2024-02-22 09:56:29 浏览: 27
好的,我可以帮你回答这个问题。
以下是用 C 语言实现的冒泡排序:
```c
#include <stdio.h>
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]) {
// 交换 arr[j] 和 arr[j+1] 的值
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
int main() {
int arr[30];
printf("随机生成的 30 个数:\n");
for (int i = 0; i < 30; i++) {
arr[i] = rand() % 100; // 产生 0 到 99 之间的随机数
printf("%d ", arr[i]);
}
printf("\n");
bubble_sort(arr, 30);
printf("冒泡排序后的结果:\n");
for (int i = 0; i < 30; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
接下来是分别采用插入、快速和希尔完成对这 30 个数的排序任务,并输出每一趟排序后的结果:
```c
#include <stdio.h>
void insert_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;
}
}
void quick_sort(int arr[], int left, int right) {
if (left >= right) return;
int pivot = arr[left];
int i = left, j = right;
while (i < j) {
while (i < j && arr[j] >= pivot) j--;
arr[i] = arr[j];
while (i < j && arr[i] <= pivot) i++;
arr[j] = arr[i];
}
arr[i] = pivot;
quick_sort(arr, left, i-1);
quick_sort(arr, i+1, right);
}
void shell_sort(int arr[], int n) {
int gap;
for (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;
}
}
}
int main() {
int arr[30];
printf("随机生成的 30 个数:\n");
for (int i = 0; i < 30; i++) {
arr[i] = rand() % 100; // 产生 0 到 99 之间的随机数
printf("%d ", arr[i]);
}
printf("\n");
insert_sort(arr, 30);
printf("插入排序后的结果:\n");
for (int i = 0; i < 30; i++) {
printf("%d ", arr[i]);
}
printf("\n");
quick_sort(arr, 0, 29);
printf("快速排序后的结果:\n");
for (int i = 0; i < 30; i++) {
printf("%d ", arr[i]);
}
printf("\n");
shell_sort(arr, 30);
printf("希尔排序后的结果:\n");
for (int i = 0; i < 30; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
希望能帮到你!
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![rtf](https://img-home.csdnimg.cn/images/20210720083624.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)