#include #include #include #if 1 typedef int element; #define format_element "%d" #else typedef double element; #define format_element "%lf" #endif void tri_a_bulle(element *tab, int taille) { int i,j; for(i=0;itab[j+1]) { element x=tab[j]; tab[j]=tab[j+1]; tab[j+1]=x; } } void tri_a_bulle2(element *tab, int taille) { int j,encore; do { encore=0; for(j=0;jtab[j+1]) { element x=tab[j]; tab[j]=tab[j+1]; tab[j+1]=x; encore=1; } } while(encore); } void tri_a_bulle3(element *tab, int taille) { int j,nt; for(;taille>1;taille=nt) { nt=0; for(j=0;jtab[j+1]) { element x=tab[j]; tab[j]=tab[nt=j+1]; tab[j+1]=x; } } } void tri_selection(element *tab, int taille) { int i,j; for(i=0;i=0 && t[j]>x;j--) t[j+1]=t[j]; t[j+1]=x; } } int partition1(element t[], int debut, int fin, int ipiv) { element pivot=t[ipiv], x; t[ipiv]=t[fin]; int i, j; for(i=j=debut;i=pivot) j--; x=t[i]; t[i]=t[j]; t[j]=x; } if(t[j]pivot) ; if(i==j) return j; t[i]=t[j]; t[j]=pivot; } } int (*partition)(element t[], int, int, int)=partition1; void tri_rapide(element t[], int debut, int fin) { if(debut1;n--) if(t[n-2]>t[n-1]) return 0; return 1; } element somme(element t[], int n) { element s=0; while(n) s+=t[--n]; return s; } typedef void (*tri)(element*,int); void test(element t[],int n,tri f,const char *nom) { element *u=malloc(n*sizeof(*u)); int i; for(i=n;i--;) u[i]=t[i]; int a=clock(); f(u,n); a=clock()-a; printf("%s trie %s %d nombres en un temps de %d/%ds\n", nom,croissant(u,n) && somme(t,n)==somme(u,n)?"bien":"mal",n,a,(int)CLOCKS_PER_SEC); free(u); } #define testri(t,n,tri) test(t,n,tri,#tri) void taleat(element t[], int n) { int i; for(i=n;i--;) t[i]=i*i%(2*n+1); } int main() { element t[]={21, 2, -1, 7, 4, 18, 3, 9, 23, 0, 1, 14, -5}, n=13; afft(t,n); tri_insertion(t,n); afft(t,n); for(n=1;n<=1000000;n*=10) { element *t=malloc(n*sizeof(*t)); taleat(t,n); testri(t,n,tri_rapide1); testri(t,n,tri_rapide2); testri(t,n,tri_rapide3); testri(t,n,tri_fusion1); testri(t,n,tri_fusion2); testri(t,n,tri_fusion3); testri(t,n,tri_fusion4); testri(t,n,tri_fusion_ite1); testri(t,n,tri_fusion_ite2); testri(t,n,tri_fusion_ite3); testri(t,n,tri_fusion_ite4); if(n>10000) continue; testri(t,n,tri_a_bulle); testri(t,n,tri_a_bulle2); testri(t,n,tri_a_bulle3); testri(t,n,tri_selection); testri(t,n,tri_insertion); free(t); } return 0; }