第1个回答 2008-09-06
template<class T>
void BubbleSort(T a[],int n)
{
bool swapped=true;
for(int i=n;i>1 && swapped;i--)
{
swapped=false;
for(int j=0;j<i-1;j++)
{
if(a[j]>a[j+1])
{
Swap(a[j],a[j+1]);
swapped=true;
}
}
}
}本回答被提问者采纳
第2个回答 2008-09-18
void sort(int a[],int n)
{
int temp;
for(int i=1;i<=n-1;i++)
for(int j=0;j<=i;j++)
{
if(a[j]>a[j+1])//比较大小,大的向下澄,小的向上冒
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
第3个回答 2008-09-06
自己多多的研究下先,你原理都那么明白了 ,想想就可以编出来了嘛。
第4个回答 2008-09-06
void Bubble(int a[],int n)
{
int n=10;
bool fast=true;
int tmp;
for (int i=0;i<9;i++)
{
for (int j=0;j<9-i;j++)
{
if (a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
fast=false;
}
}
if(fast) return;
}
}