Sort by fractions and alphabetically by name
2#include <string.h>3#include <stdio.h>4 structperson{5 Char*name;6 intscore;7 };8 9 intInitstructPerson *p,Char*name,intscore)Ten { OneP->name =name; AP->score =score; - } - the intFind_max (structPerson *p) - { - intI, J; - intmax = p->score; + for(i=0; i<6; i++) - { + if((p+i)->score >max) Amax = (p+i)score; at } - - returnMax; - } - - intSortstructPerson *p) in { - intI=0, j=0, max=0, maxi=0, k=0; to Char*arr; + for(i=0; i<6; i++) - { theMaxi=i; *max = (p+i)score; $ for(j=i+1; j<6; J + +)Panax Notoginseng { - if((p+j)->score >max) the { +max = (p+j)score; AMaxi =J; the } + } -arr = (p+i)name; $(p+i)->name = (P+maxi)name; $(p+maxi)->name =arr; - -K = (p+i)score; the(p+i)->score = (P+maxi)score; -(p+maxi)->score =K;Wuyi theprintf"sss is%d \ n", (P+i)score); - } Wu - } About $ intSort1 (structPerson *p) - { - intI, J, K, M; - Char*arr; A + for(i=0; i<6; i++) the { - for(j=i+1; j<6; J + +) $ { them = strcmp ((p+i)->name, (P+J),name); the if(M >0) the { thearr = (p+i)name; -(p+i)->name = (p+j)name; in(p+j)->name =arr; the theK = (p+i)score; About(p+i)->score = (p+j)score; the(p+j)->score =K; the } the}
Results:
SSS is 98-SSS is-am-AM-am--------- 8jim, 79lucy, 67suoluo, 60tom, 98tubi, 65avg are 76max is 98
here the score is sorted by selection, the alphabet is bubble sort
Note: When using the selection sort Maxi to assign a value to the current I, each time to start from the next, the algorithm to think more practice.
Beginner's notes-simple sort exercises