void BubbleSort(int *a ,int n)
{
    
for(int i=0;i<n-1;i++)
    {
        
for(int j=0;j<n-1-i;j++)
        {
            
if(a[j]>a[j+1])
            { 
int temp=a[j];
                a[j]
=a[j+1];
                a[j
+1]=temp;
            }
        }
    }
}
void BubbleSort(int *a,int n)
{
    
for(int i=0;i<n-1;i++)
    {
        
for(int j=n-1;j>=i+1;j--)
        {
            
if(a[j]<a[j-1])
            {
                
int temp=a[j];
                a[j]
=a[j-1];
                a[j
-1]=temp;
            }

        }
    }
}

两者的差别在于前一种实现方案:是逐渐将大数“沉底”,而第二种实现方案,是逐渐将小数“上浮”。
验证主函数如下
#include<iostream>
using namespace std;
void main()
{  void BubbleSort(int *A ,int n);
   int a[6]={3,7,4,5,4,9};
   BubbleSort(a,6);
   for(int k=0;k<6;k++)
   {
    cout<<a[k]<<" ";
   }
   cout<<endl;
 char f;
 cin>>f;
}
posted on 2009-08-12 18:58  finallyly  阅读(215)  评论(0编辑  收藏  举报