C语言的冒泡排序和快速排序算法使用实例

2020-01-06 13:40:40于丽
  •   int main()  
  • {   struct student students[1001];  
  • int i, n;    
  • while(scanf("%d",&n) != EOF)   {  
  • //学生成绩赋值   for(i = 0; i < n; i ++)  
  • {   scanf("%s%d%d",students[i].name, &students[i].age, &students[i].grade);  
  • }    
  • //快速排序   quicksort(students, 0, n-1);  
  •   //打印输出  
  • for(i = 0; i < n; i ++)   {  
  • printf("%s %d %dn",students[i].name, students[i].age, students[i].grade);   }  
  • }    
  • return 0;   }  
  •   void quicksort(struct student *A, int begin, int end)  
  • {   int pivot;  
  •   if(begin < end)  
  • {   pivot = partition(A, begin, end);  
  • quicksort(A, begin, pivot - 1);   quicksort(A, pivot + 1, end);  
  • }   }  
  •   int partition(struct student *A, int left, int right)  
  • {   struct student stand = A[left];  
  •   while(left < right)  
  • {   while(left < right && (A[right].grade > stand.grade || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) > 0) || (A[right].grade == stand.grade && strcmp(A[right].name,stand.name) == 0 && A[right].age > stand.age ) ) )  
  • {   right --;  
  • }   if(left < right)  
  • {   A[left ++] = A[right];  
  • }    
  • while(left < right && (A[left].grade < stand.grade || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) < 0) || (A[left].grade == stand.grade && strcmp(A[left].name,stand.name) == 0 && A[left].age < stand.age ) ) )   {