数据结构——冒泡排序

  交换排序的基本思想是:两两比较待排序记录的关键字,发现两个记录的次序相反时即进行交换,直到没有反序的记录为止。
  应用交换排序基本思想的主要排序方法有:冒泡排序和快速排序。

//冒泡排序算法

#include
<iostream>
using namespace std;


void Bubblesort(int num[],int n)
{
int i,j,temp;
for(i=n;i>0;i--)
for(j=0;j<i-1;j++) //注意i和j的范围
{
if(num[j]>num[j+1])
{
temp
=num[j];
num[j]
=num[j+1];
num[j
+1]=temp;
}
//======================输出冒泡排序的过程
cout<<""<<i<<"";
for(int k=0;k<i;k++)
cout
<<num[k]<<" ";
cout
<<endl;
}
}



int main()
{
FILE
*fin=fopen("8.1.2.txt","r");
int num[80],i=0;
while(fscanf(fin,"%d",&num[i])!=EOF)
i
++;
int n=i;

Bubblesort(num,n);

for(i=0;i<n;i++)
{
if(i%5==0)
cout
<<endl;
cout
<<num[i]<<" ";
}
cout
<<endl;
fclose(fin);
return 0;
}

 

posted @ 2010-07-14 15:15  忧国忧铭  Views(238)  Comments(0)    收藏  举报