帮我写一个C++的冒泡排序代码
时间: 2023-03-25 18:01:29 浏览: 49
c++冒泡排序代码
#include<stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("输入数组元素个数: ");
scanf("%d", &n);
printf("输入 %d 个数字: ", 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("排序后的数组: \n");
for (c = 0; c < n; c++)
printf("%d\n", array[c]);
return 0;
}
阅读全文