INTERCLASARE
// Program sortare prin interclasare. Datele se gasesc in fisierul SIR.DAT #include<stdio.h> #include<conio.h> #define N 100 void Interclasare(int a[N], int p, int q, int r) { int k=0,i=p,j=q+1,l,c[N]; while ((i<=q)&&(j<=r)) { if (a[i]<a[j]) { c[k]=a[i]; k++; i++; } else { c[k]=a[j]; j++; k++; } } if (i>q) for(l=j;l<=r;l++) { c[k]=a[l]; k++; } else for(l=i;l<=q;l++) { c[k]=a[l]; k++; } j=p; for (i=0;i<(r-p+1);i++) { a[j]=c[i]; j++; } } void Sortare_Interclasare(int a[N], int p, int r) { int q; if (p<r) { q=(p+r)/2; Sortare_Interclasare(a,p,q); Sortare_Interclasare(a,q+1,r); Interclasare(a,p,q,r); } } void main() { int sir[N],n,k; FILE *fp; clrscr(); if((fp=fopen("SIR.DAT","r"))==NULL) { puts("ERROR"); return; } printf("Inainte de sortare:"); fscanf(fp,"%d",&n); for (k=0;k<n;k++) { fscanf(fp,"%d",&sir[k]); printf("\n\ta[%d] = %d",k,sir[k]); } fclose(fp); printf("\n\n\n"); Sortare_Interclasare(sir,0,n-1); printf("Dupa sortare:"); for (k=0;k<n;k++) printf("\n\ta[%d] = %d",k,sir[k]); getch(); }
1,934 total views, 1 views today