1 / 12

习题3.1

while(j<=k) { m=(j+k)/2; if(x<a[m]) k=m-1; else j=m+1; } for(k=i-1;k>=j;k--) a[k+1]=a[k]; a[j]=x; } }. 习题3.1. #define M 100 int a[M]; void bisort(a, n) int a[], n; { int x,i,j,k,m; for(i=1;i<n;i++) { x=a[i]; j=0;

xiang
Télécharger la présentation

习题3.1

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. while(j<=k) { m=(j+k)/2; if(x<a[m]) k=m-1; else j=m+1; } for(k=i-1;k>=j;k--) a[k+1]=a[k]; a[j]=x; } } 习题3.1 #define M 100 int a[M]; void bisort(a, n) int a[], n; { int x,i,j,k,m; for(i=1;i<n;i++) { x=a[i]; j=0; k=i-1;

  2. typedef struct node{ int data; struct node *link; } NODE; NODE **phead; voidinsertion_l_sort(phead) NODE **phead; {NODE *p,*q,*s,*r; p=*phead; q=p->link; while(q!=NULL) { s=*phead; while(s!=q&& s->data<=q->data) { r=s; s=s->link; } if(s==q) p=q; else { p->link=q->link; q->link=s; if(s==*phead) *phead=q; else r->link=q; } q=p->link; } } 习题3.2

  3. void selection_l_sort(phead) NODE **phead; { NODE *p,*q,*r,*s; r=(NODE *)malloc (sizeof(NODE)); r->link=*phead; *phead=r; while(r->link!=NULL) { q=s=r->link; while(q->link!=NULL) { if(q->link->data<s->data) { p=q; s=q->link; } q=q->link; } p->link=s->link; s->link=r->link; r->link=s; r=r->link; } p=*phead; *phead=p->link; free(p); } 习题3.3

  4. void double_bubble_sort(a,n) int a[]; int n; { int i,j,t; n--; while(n>0) { j=0; for(i=0;i<n;i++) if(a[i]>a[i+1]) { t=a[i]; a[i]=a[i+1]; a[i+1]=t; j=i; } n=j; for(i=n-1;i>=0 &&a[i]<a[i+1];i--) n--; for(i=n-1;i>=0;i--) if(a[i]>a[i+1]) { t=a[i]; a[i]=a[i+1]; a[i+1]=t; } } } 习题3.4

  5. void merge_l(p,q,r,ph,ppt) NODE *p,*q,*r,*ph,**ppt; { NODE *pb, *pq; pb=ph; pq=q; while(p!=q&&pq!=r) { if(p->data<=pq->data) { pb->link=p; p=p->link; } else { pb->link=pq; pq=pq->link; } pb=pb->link; } while(p!=q) { pb->link=p; p=p->link; pb=pb->link; } while(pq!=r) { pb->link=pq; pq=pq->link; pb=pb->link; } *ppt=pb; } 习题3.5上

  6. int mpass_l(pa,pb,k) NODE **pa,**pb; int k; { NODE *p,*q,*r,*ph,*pt; int i,flag=0; ph=(NODE *)malloc (sizeof(NODE)); ph->link=*pb; *pb=ph; q=p=*pa; while(p!=NULL) { for(i=0;q!=NULL&&i<k;i++) q=q->link; if(flag==0&&q==NULL) flag=9999; r=q; for(i=0;r!=NULL&&i<k;i++) r=r->link; merge(p,q,r,ph,&pt); q=p=r; ph=pt; flag++; } ph=*pb; *pb=ph->link; free(ph); if(flag>9000) return 1; else return 0; } 习题3.5中

  7. void merge_l_sort(pa) NODE **pa; { NODE *b; int k, flag=1; k=1; while(flag) { if(mpass_l(pa,&b,k)) flag=0; k*=2; if(mpass_l(&b,pa,k)) flag=0; k*=2; } } 习题3.5下

  8. #define MAXN 100 void merge(a,b,h,m,n) int a[ ],b[ ]; int h,m,n; { int i,j,k; i=h; j=m+1; k=h; while(i<=m&&j<=n) if(a[i]<=a[j]) b[k++]=a[i++]; else b[k++]=a[j++]; while(i<=m) b[k++]=a[i++]; while(j<=n) b[k++]=a[j++]; } void count(a,n,c) int a[ ],c[ ]; int n; { int x,j,k; c[0]=0; j=1; for(k=0;k<n;k++) if(a[k]>a[k+1]) c[j++]=k; c[j]=n-1; } 习题3.6上

  9. void mpass(a,b,c,n) int a[ ],b[ ],c[ ]; int n; { int i,j,k k=1; j=0; i=1; while(c[i]!=n-1) { merge(a,b,j,c[i],c[i+1]); j=c[i+1]+1; c[k++]=c[i+1]; if(c[i+1]==n-1) break; i=i+2; } if(c[i]==n-1) { for(;j<n;j++) b[j]=a[j]; c[k]=n-1; } } 习题3.6中

  10. void merge_sort(a,n) int a[ ]; int n; { int b[MAXN],c[MAXN]; count(a,n,c); while(c[1]!=n-1) { mpass(a,b,c,n); mpass(b,a,c,n); } } 习题3.6下

  11. #define M 100 void quick(a,n) int a[ ]; int n; { int b[M][2]; int i,j,ii,jj,top,t; top=0; b[top][0]=0; b[top++][1]=n; while(k!=0) { i=ii=b[--top][0]; j=jj=b[top][1]; t=a[i]; while(i!=j) { while(i<j&&a[j]>t) j--; if(i<j) a[i++]=a[j]; while(i<j&&a[i]<=t) i++; if(i<j) a[j--]=a[i]; } a[i]=t; if(ii<i-1) { b[top][0]=ii; b[top++][1]=i-1; } if(i+1<jj) { b[top][0]=i+1; b[top++][1]=jj; } } } 习题3.7

  12. 12,2,16,30,8,28,4,10,20,6,18 2,12,16,30,8,28,4,10,20,6,18 2,12,16,30,8,28,4,10,20,6,18 2,12,16,30,8,28,4,10,20,6,18 2,8,12,16,30,28,4,10,20,6,18 2,8,12,16,28,30,4,10,20,6,18 2,4,8,12,16,28,30,10,20,6,18 2,4,8,10,12,16,28,30,20,6,18 2,4,8,10,12,16,20,28,30,6,18 2,4,6,8,10,12,16,20,28,30,18 2,4,6,8,10,12,16,18,20,28,30 3.8 (1)插入排序

More Related