用c++写一个冒泡排序
时间: 2023-03-16 17:43:40 浏览: 112
C++ 冒泡排序
4星 · 用户满意度95%
我可以为你提供一个用C++编写的冒泡排序程序的示例:
#include<iostream>
using namespace std; int main()
{
int n, array[50], c, d, swap;
cout << "Enter number of elements\n" ;
cin >> n;
cout << "Enter " << n << " integers\n" ;
for (c = 0; c < n; c++)
cin >> array[c];
for (c = 0 ; c < ( n - 1 ); c++)
{
for (d = 0 ; d < n - c - 1; d++)
{
if (array[d] > array[d+1])
{
swap = array[d];
array[d] = array[d+1];
array[d+1] = swap;
}
}
}
cout << "Sorted list in ascending order:\n";
for (c = 0; c < n; c++)
cout << array[c] << " ";
return 0;
}
阅读全文