小彭屋

导航

冒泡排序法

//代码  在vc6.0上面编译通过

#include <iostream>
using namespace std;
void BubbleSort(int arr[], int n)
{
 int i=0,j=0;    
 for(i=0;i<n;i++)
 {
  for(j=0;j<n-1-i;j++)
  {
   if(arr[j]>arr[j+1])
   {
    int temp;
    temp = arr[j];
    arr[j] = arr[j+1];
    arr[j+1] = temp;
   }            
  }       
 } 
}


int main()
{
 int a[] = {4,2,9,5,8};
 for (int i=0;i<5;i++)
 {
  cout<<a[i]<<"\t";
 }
 BubbleSort(a,5);
 cout<<endl;
 for (int j=0;j<5;j++)
 {
  cout<<a[j]<<"\t";
 }
    cout<<endl;
 return 0;
}

posted on 2013-06-07 17:32  小彭屋  阅读(125)  评论(0)    收藏  举报