请用C++写一段程序,实现冒泡排序
时间: 2023-03-15 11:46:18 浏览: 115
冒泡排序的C++实现
#include <stdio.h>
int main()
{
int array[100], n, c, d, swap;
printf("请输入元素个数:");
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;
}
阅读全文