利用结构体来写冒泡排序问题

#include
#include
#define MAX_SIZE 10

typedef struct
{
	int r[MAX_SIZE + 1];
	int length;
} SqList;


void swap( SqList* L, int i, int j )
{
	int temp = L->r[i];
	L->r[i] = L->r[j];
	L->r[j] = temp;
}
void BubbleSort( SqList* L )
{
	int i, j;
	for( i = 1; i < L->length; ++i )
		for( j = i + 1; j <= L->length; ++j )
			if( L->r[i] > L->r[j] ) swap( L, i, j );
	return;
}
int main()
{
	int length, i;
	scanf( "%d", &length );
	SqList* L = ( SqList* ) malloc( sizeof( SqList ) );
	L->length = length;
	for( i = 1; i <= length; ++i )
		scanf( "%d", &L->r[i] );
	BubbleSort( L );
	for( i = 1; i <= length; ++i )
		printf( "%d ", L->r[i] );
	return 0;
}

你可能感兴趣的:(C语言,算法系列)