帮我写c++泡泡排序
时间: 2023-09-09 07:05:04 浏览: 61
### 回答1:
答案:
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("Enter number of elements\n");
scanf("%d", &n);
printf("Enter %d integers\n", n);
for (c = 0; c < n; c++)
scanf("%d", &array[c]);
for (c = 0 ; c < n - 1; c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1]) /* For decreasing order use < */
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
printf("Sorted list in ascending order:\n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
### 回答2:
泡泡排序(Bubble Sort)是一种简单的排序算法。它通过迭代地比较相邻的元素,并且如果顺序不正确则交换它们的位置,直到整个序列排序完成。
下面是C语言中使用泡泡排序算法对一个整型数组进行排序的示例代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int size) {
for (int i = 0; i < size-1; i++) {
for (int j = 0; j < size-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[] = {64, 34, 25, 12, 22, 11, 90};
int size = sizeof(arr) / sizeof(arr[0]);
bubbleSort(arr, size);
printf("排序后的数组:\n");
for (int i=0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
return 0;
}
```
以上代码中,bubbleSort函数使用嵌套的for循环来迭代地比较相邻的元素,如果顺序不正确则交换它们的位置。在每一轮循环中,将最大的元素“冒泡”到数组末尾,因此内层循环每次减少一次遍历的次数。最后,通过main函数调用bubbleSort进行排序,并打印排序后的数组。
运行结果:
排序后的数组:
11 12 22 25 34 64 90
这是一个简单但效率较低的排序算法,对于较大规模的数据排序可能会比较耗时。
### 回答3:
泡泡排序(Bubble Sort)是一种简单的排序算法。其基本思想是依次比较相邻的两个元素,根据大小关系进行交换,使得最大(或最小)的元素逐渐“浮”到数列的末端。经过一轮比较后,最大(或最小)的元素就会位于数列最后一个位置,再进行下一轮的比较交换,直到所有元素都按照要求排序完毕。
下面是用C语言实现泡泡排序的示例代码:
```c
#include <stdio.h>
void bubbleSort(int arr[], int n) {
for (int i = 0; i < n-1; i++) { // 进行n-1轮的比较和交换
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[] = {64, 34, 25, 12, 22, 11, 90}; // 待排序的数组
int n = sizeof(arr) / sizeof(arr[0]); // 数组的长度
bubbleSort(arr, n); // 调用泡泡排序函数
printf("排序后的数组:");
for (int i = 0; i < n; i++) {
printf("%d ", arr[i]);
}
return 0;
}
```
上述代码中,首先定义了一个实现泡泡排序的函数`bubbleSort`。函数通过两重循环遍历待排序的数组,通过比较相邻元素的大小,进行交换操作。外层循环控制进行比较交换的轮数,内层循环用于比较相邻元素并进行交换。最后在`main`函数中调用`bubbleSort`函数进行排序,并输出排序结果。
以上就是用C语言实现泡泡排序的简单示例代码,希望能对你有所帮助。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.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)