#include #include #include #include void quicksort(int tableau[], int premier, int dernier){ int i, j, pivot, temp; if(premier < dernier){ pivot = premier; i = premier; j = dernier ; while(i < j){ while(tableau[i] <= tableau[pivot] && i < dernier) i++; while(tableau[j] > tableau[pivot]) j--; if(i < j){ temp = tableau[i]; tableau[i] = tableau[j]; tableau[j] = temp; } } temp = tableau[pivot]; tableau[pivot] = tableau[j]; tableau[j] = temp; quicksort(tableau, premier, j-1); quicksort(tableau, j+1, dernier); } } int comptemots(char* fichier){ FILE *inTube; long nombreMots = 0; char commande[64] = "wc -w "; strcat(commande, fichier); inTube = popen(commande, "r"); if (inTube != NULL) { fscanf(inTube, "%ld", &nombreMots); pclose(inTube); } // printf("%ld\n", nombreMots); return(nombreMots); } int main(int argc, char* argv[]){ int x, i, nombreMots = 0; long clk_tck = CLOCKS_PER_SEC; clock_t t1, t2; nombreMots = comptemots(argv[1]); FILE *fp; int *tableau = malloc(nombreMots * sizeof(int)); fp = fopen (argv[1], "r"); for (i = 0; i < nombreMots; i++){ fscanf(fp, "%d", &x); *(tableau + i) = x; } fclose (fp); t1 = clock(); quicksort(tableau, 0, nombreMots - 1); t2 = clock(); for (i = 0; i < nombreMots; i++) printf(" %d\n", tableau[i]); free(tableau); (void)printf("Nb ticks/seconde = %ld, Nb ticks depart : %ld, " "Nb ticks final : %ld\n", clk_tck, (long)t1, (long)t2); (void)printf("Temps consomme (s) : %lf \n", (double)(t2-t1)/(double)clk_tck); return 0; }