请用c++实现冒泡排序
时间: 2023-03-09 10:43:52 浏览: 132
我可以用下面的c程序来实现冒泡排序:#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("输入要排序的元素个数\n");
scanf("%d", &n);
printf("输入 %d 个元素\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("排序后的结果:\n");
for ( c = 0 ; c < n ; c++ )
printf("%d\n", array[c]);
return 0;
}
阅读全文