数据结构(C++)冒泡排序的代码

最好有注释...
2026年09月24日 23:18
有2个网友回答
网友(1):

冒泡排序数组s[]的元素,从大到小。

for(int i=1;i!=n;i++) //外层循环
{
for(int j=0;j!=n-i;j++) //内层循环
{
if(s[j] {
temp=s[j]; //符合条件,交换
s[j]=s[j+1];
s[j+1]=temp;
}
}
}

网友(2):

#include
int a[20];
void bubblesort(int a[],int n)
{
int i,j;
int temp;
for(i=0;i {
for(j=n-1;j>i;j--)
if(a[j] {
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
void main()
{

int i,n,max=0;
cout<<"input the number n:"< cin>>n;
cout<<"input the numbers a[i]:"< for(i=0;i cin>>a[i];
bubblesort(a, n);
for(i=0;i cout<<" "< cout<}
希望能帮到你