#define TRUE 1
#define FALSE 0
typedef int KeyType;
typedef struct
{
KeyType key;
OtherType other_data;
}RecordType;
void BubbleSort(RecordType r[],int length)
{/*对记录数组r做冒泡排序,length为数组的长度
是稳定的排序方法
时间复杂度为O(n的平方)
空间复杂度为O(1)*/
int i,j,n,change;
for(i=1,n=length-1,change=FALSE;i<=n&&change==FALSE;n--)
{
change=TRUE;
for(j=1;j<=n;j++)
if(r[j].key>r[j+1].key)
{
r[0]=r[j];
r[j]=r[j+1];
r[j+1]=r[0];
change=FALSE;
}
}
}
温馨提示:答案为网友推荐,仅供参考