cosmy
Moderator
Inregistrat: acum 16 ani
Postari: 33
|
|
Sortarea prin numarare
#include<stdio.h> #include<math.h> #include<conio.h>
int n,i,j,z[10],c[10],x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=0;i<n;i++){ fscanf(fin,"%d",&x[i]);} }
void sort(){ for (i=0;i<n;i++) c[i]=0; for (i=1;i<n;i++) for (j=0;j<i;j++) if (x[i]<x[j]) c[j]++; else c[i]++; for (i=0;i<n;i++) z[c[i]]=x[i];}
void afisare(){ for (i=0;i<n;i++) fprintf(fout,"%d \t",z[i]);}
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); fclose(fin); sort(); afisare(); fclose(fout); }
Sortarea prin insertie directa
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,t,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=1;i<=n;i++) fscanf(fin,"%d",&x[i]); }
void insertie(){ for (i=2;i<=n;i++){ j=i-1;t=0;aux=x[i]; do{ if (aux<x[j]) {x[j+1]=x[j]; j--;} else t=1; }while ((j!=0) && (!t)); x[j+1]=aux;}}
void afisare(){ for (i=1;i<=n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); insertie(); afisare(); fclose(fin); fclose(fout); }
Sortarea prin insertie binara
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,inc,sf,med,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=1;i<=n;i++) fscanf(fin,"%d",&x[i]); }
void insertie_binara(){ for (i=2;i<=n;i++){ aux=x[i];inc=0;sf=i-1; while (inc<=sf){med= (inc+sf)/2; if (aux<x[med]) sf=med-1; else inc=med+1;} for (j=i-1;j>=inc;j--) x[j+1]=x[j]; x[inc]=aux;} }
void afisare(){ for (i=1;i<=n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); insertie_binara(); afisare(); fclose(fin); fclose(fout); }
Metoda Bulelor_1
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,inc,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=0;i<n;i++) fscanf(fin,"%d",&x[i]); }
void bule1(){ for (j=0;j<n-1;j++) for (i=0;i<n-1;i++) if (x[i]>x[i+1]){aux=x[i]; x[i]=x[i+1]; x[i+1]=aux;} }
void afisare(){ for (i=0;i<n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); bule1(); afisare(); fclose(fin); fclose(fout); }
Metoda bulelor 2
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,inc,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=0;i<n;i++) fscanf(fin,"%d",&x[i]); }
void bule2(){ int ld; ld=n-1; do{ for (i=0;i<ld;i++) if (x[i]>x[i+1]){aux=x[i]; x[i]=x[i+1]; x[i+1]=aux;} ld--;} while(ld);}
void afisare(){ for (i=0;i<n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); bule2(); afisare(); fclose(fin); fclose(fout); }
Metoda bulelor 3
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,inc,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=0;i<n;i++) fscanf(fin,"%d",&x[i]); }
void bule3(){ int inv; do{inv=0; for (i=0;i<n-1;i++) if (x[i]>x[i+1]){aux=x[i]; x[i]=x[i+1]; x[i+1]=aux;inv=1;} } while(inv);}
void afisare(){ for (i=0;i<n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); bule3(); afisare(); fclose(fin); fclose(fout); }
Sortarea prin selectie
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,inc,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=0;i<n;i++) fscanf(fin,"%d",&x[i]); }
void selectie(){ int l,min; for (i=0;i<n-1;i++){l=i;min=x[i]; for (j=i+1;j<n;j++) if (x[j]<min){min=x[j];l=j;} x[l]=x[i];x[i]=min;}}
void afisare(){ for (i=0;i<n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); selectie(); afisare(); fclose(fin); fclose(fout); }
Sortarea prin interclasare
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,inc,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=0;i<n;i++) fscanf(fin,"%d",&x[i]); }
void interclasare(int inf,int med,int sup){ int k,l,y[10]; i=inf;j=med+1;k=inf; while ((i<=med)&&(j<=sup)){ if (x[i]<x[j]) {y[k]=x[i];i++;} else {y[k]=x[j];j++;} k++;} for (l=i;l<=med;l++) {y[k]=x[l];k++;} for (l=j;l<=sup;l++) {y[k]=x[l];k++;} for (l=inf;l<=sup;l++) x[l]=y[l];}
void sort(int inf,int sup){ int med; if (inf<sup){med= (inf+sup)/2; sort(inf,med); sort(med+1,sup); interclasare(inf,med,sup);} }
void afisare(){ for (i=0;i<n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); sort(0,n-1); afisare(); fclose(fin); fclose(fout); }
Quick-sort
#include<stdio.h> #include<conio.h> #include<math.h>
int n,i,j,inc,aux,x[10]; FILE *fin,*fout;
void creare(){ fscanf(fin,"%d",&n); for (i=0;i<n;i++) fscanf(fin,"%d",&x[i]); }
void sort(int p,int u){ i=p;j=u; if (i>=j) return; while (i<j){ while (x[i]<x[j]) i++; if (x[i]>x[j]){aux=x[i];x[i]=x[j];x[j]=aux;j--;} while (x[i]<x[j]) j--; if (x[i]>x[j]){aux=x[i];x[i]=x[j];x[j]=aux;i++;} } sort(p,i-1); sort(i+1,u);}
void afisare(){ for (i=0;i<n;i++) fprintf(fout,"%d \t",x[i]); }
void main(){ fin=fopen("in.txt","r" ); fout=fopen("out.txt","w" ); creare(); sort(0,n-1); afisare(); fclose(fin); fclose(fout); }
39KB
|
|