冒泡排序举例

#include
template
void Swap(T &x,T &y)
{
    T temp;
    temp=x;
    x=y;
    y=temp;
}

template
void BubbleSort(T A[],int n)
{
  int i,j;
  int lastExchangeIndex;
  i=n-1;
  while(i>0)
  {
    lastExchangeIndex=0;
    for(j=0;j       if(A[j+1]      {
     Swap(A[j],A[j+1]);
     lastExchangeIndex=j;
   }
   i=lastExchangeIndex;
   for(int k=0;k    cout<    cout<   }
}
 
int main()
{
  int data1[]={20,19,18,3,5,7,9,11,13,15,17,2,4,6,8,10,12,14,16,1};
  cout << "排序前的数据:" << endl;
  for(int i=0;i<20;i++)
  cout << data1[i] << " ";
  cout << endl;
  cout << "开始排序..." << endl;
  BubbleSort(data1, 20);
  cout << "排序后的数据:" << endl;
  for(int i=0;i<20;i++)
  cout << data1[i] << " ";
  cout << endl;
}

你可能感兴趣的:(冒泡排序举例)