1 void rank_list(SqList &L) 2 { 3 student p; 4 int i,j; 5 for( i=1;i<L.length;++i) 6 if(L.elem[i].average>L.elem[i-1].average) 7 { 8 p=L.elem[i]; 9 L.elem[i]=L.elem[i-1]; //赋值的都是地址 10 for( j=i-1;j>=0&&L.elem[i].average<L.elem[j].average;--j)//老师说这块是j=i-2,但是i从1开始 11 L.elem[j+1]=L.elem[j]; 12 L.elem[j+1]=p; 13 } 14 15 student *t; 16 i=0; 17 for(t=L.elem;t<L.elem+L.length;t++) //给order赋值 18 { 19 L.elem[i].order=i+1; 20 i++; 21 } 22 /* 23 //折半插入排序 24 student p; 25 int i,j,low,high,mid; 26 for(i=1;i<L.length;++i) 27 { 28 p=L.elem[i]; 29 low=0;high=i-1; 30 while(low<=high) 31 { 32 mid=(low+high)/2; 33 if(L.elem[i].average>L.elem[mid].average) 34 high=mid-1; 35 else 36 low=mid+1; 37 } 38 for(j=i-1;j>=high+1;--j) 39 L.elem[j+1]=L.elem[j]; 40 L.elem[high+1]=p; 41 } 42 student *t; 43 i=0; 44 for(t=L.elem;t<L.elem+L.length;t++) //给order赋值 45 { 46 L.elem[i].order=i+1; 47 i++; 48 } 49 50 希尔排序 51 int i,j,gap; 52 for(gap=L.length/2;gap>0;gap/=2) 53 for(i=gap;i<L.length;i++) 54 for(j=i-gap;j>=0&&L.elem[j].average<L.elem[gap+j].average;j-=gap) 55 { 56 student p; 57 p=L.elem[j]; 58 L.elem[j]=L.elem[j+gap]; 59 L.elem[j+gap]=p; 60 } 61 62 冒泡排序 63 student *p,*q; 64 p=L.elem;q=L.elem; 65 for(p;p<L.elem+L.length;p++) 66 { 67 for(q=p;q<L.elem+L.length-1;q++) 68 { 69 if(q->average<(q+1)->average) 70 { 71 student t; //这块必须这么写,不然排不好,队成员一个一个换太麻烦 72 t=*q; 73 *q=*(q+1); 74 *(q+1)=t; 75 } 76 } 77 } 78 79 80 student *t; 81 i=0; 82 for(t=L.elem;t<L.elem+L.length;t++) //给order赋值 83 { 84 L.elem[i].order=i+1; 85 i++; 86 } 87 } 88 89 快排 90 void quick_sort(SqList &L,int l,int r) 91 { 92 int i,j; 93 if(r<l) 94 { 95 j=0;r=L.length;int x=L.elem[l].average; 96 while(i<j) 97 { 98 while(i<j&&L.elem[j].average>=x) 99 j--; 100 if(i<j) 101 L.elem[i++].average=L.elem[j].average; 102 while(i>j&&L.elem[i].average<x) 103 i++; 104 if(i<j) 105 L.elem[j--].average=L.elem[i].average; 106 } 107 L.elem[i].average=x; 108 quick_sort(L,l,i-1); 109 quick_sort(L,i+1,r); 110 } 111 }
void rank_list(SqList &L) { student p; int i,j; for( i=1;i<L.length;++i) if(L.elem[i].average>L.elem[i-1].average) { p=L.elem[i]; L.elem[i]=L.elem[i-1]; //赋值的都是地址 for( j=i-1;j>=0&&L.elem[i].average<L.elem[j].average;--j)//老师说这块是j=i-2,但是i从1开始 L.elem[j+1]=L.elem[j]; L.elem[j+1]=p; } student *t; i=0; for(t=L.elem;t<L.elem+L.length;t++) //给order赋值 { L.elem[i].order=i+1; i++; } /* //折半插入排序 student p; int i,j,low,high,mid; for(i=1;i<L.length;++i) { p=L.elem[i]; low=0;high=i-1; while(low<=high) { mid=(low+high)/2; if(L.elem[i].average>L.elem[mid].average) high=mid-1; else low=mid+1; } for(j=i-1;j>=high+1;--j) L.elem[j+1]=L.elem[j]; L.elem[high+1]=p; } student *t; i=0; for(t=L.elem;t<L.elem+L.length;t++) //给order赋值 { L.elem[i].order=i+1; i++; } 希尔排序 int i,j,gap; for(gap=L.length/2;gap>0;gap/=2) for(i=gap;i<L.length;i++) for(j=i-gap;j>=0&&L.elem[j].average<L.elem[gap+j].average;j-=gap) { student p; p=L.elem[j]; L.elem[j]=L.elem[j+gap]; L.elem[j+gap]=p; } 冒泡排序 student *p,*q; p=L.elem;q=L.elem; for(p;p<L.elem+L.length;p++) { for(q=p;q<L.elem+L.length-1;q++) { if(q->average<(q+1)->average) { student t; //这块必须这么写,不然排不好,队成员一个一个换太麻烦 t=*q; *q=*(q+1); *(q+1)=t; } } } student *t; i=0; for(t=L.elem;t<L.elem+L.length;t++) //给order赋值 { L.elem[i].order=i+1; i++; } } 快排 void quick_sort(SqList &L,int l,int r) { int i,j; if(r<l) { j=0;r=L.length;int x=L.elem[l].average; while(i<j) { while(i<j&&L.elem[j].average>=x) j--; if(i<j) L.elem[i++].average=L.elem[j].average; while(i>j&&L.elem[i].average<x) i++; if(i<j) L.elem[j--].average=L.elem[i].average; } L.elem[i].average=x; quick_sort(L,l,i-1); quick_sort(L,i+1,r); } }