冒泡排序

 

#include"stdio.h"
#include"string.h"
#define N 50
struct student{
 int number;
 char name[20];
 int score;
 };
void paixu(struct student a[],int n);//函数声明

 

int main(void)
{
 struct student a[N];
 int i=0,j,k;

 while(1){

  printf("please input number,name and score:\n");
  scanf("%d%s%d",&a[i].number,&a[i].name,&a[i].score);

  if(a[i].number==0)
   {
    //printf("No records!!!");;  
    break;
   }
  i++;

  }//while

  if(a[0].number==0)
   printf("No records!!!\n");
  else{
  
  paixu(a,i-1);//函数调用
  printf("The student's data is:\n");
  for(j=0;j<i;j++)
  printf("%d  %s  %d\n",a[j].number,a[j].name,a[j].score);
  }
  return 0;
}


void paixu(struct student a[],int n)//函数定义
{
 int i,j,b,d;
 struct student c[N];
 for(i=1;i<n;i++)
  {
   for(j=0;j<n-i;j++)
    if(a[j].number>a[j+1].number)
    {
     b=a[j].number;
     a[j].number=a[j+1].number;
     a[j+1].number=b;

     strcpy(c[j].name,a[j].name);
     strcpy(a[j].name,a[j+1].name);
     strcpy(a[j+1].name,c[j].name);

     d=a[j].score;
     a[j].score=a[j+1].score;
     a[j+1].score=d;
    }  

  }//for

}

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