diff --git a/Projet/Makefile b/Projet/Makefile new file mode 100644 index 0000000000000000000000000000000000000000..01fc0ab4bd50ae5aa31829a3485ab9086a14f8fe --- /dev/null +++ b/Projet/Makefile @@ -0,0 +1,18 @@ +all: main + +main : tri_fusion.o tri_insertion.o tri_rapide.o utils.o main.c + gcc -o main -Wall utils.o tri_fusion.o tri_insertion.o tri_rapide.o -g main.c + +tri_fusion.o : tri_fusion.c tri_fusion.h + gcc -c -Wall -g tri_fusion.c + +tri_insertion.o : tri_insertion.c tri_insertion.h + gcc -c -Wall -g tri_insertion.c + +tri_rapide.o : tri_rapide.c tri_rapide.h + gcc -c -Wall -g tri_rapide.c + +utils.o : utils.c utils.h + gcc -c -g utils.c +clean : + rm *.o diff --git a/Projet/ggossaprodansR.png b/Projet/ggossaprodansR.png new file mode 100644 index 0000000000000000000000000000000000000000..90103439e1c741904d603f17a0f2e33373416a53 Binary files /dev/null and b/Projet/ggossaprodansR.png differ diff --git a/Projet/main.c b/Projet/main.c new file mode 100644 index 0000000000000000000000000000000000000000..a35c68de3a11fe0b49bb9bbb5af6618c7d41fcdc --- /dev/null +++ b/Projet/main.c @@ -0,0 +1,56 @@ +#include <stdio.h> +#include <stdlib.h> +#include "tri_insertion.h" +#include "tri_rapide.h" +#include "tri_fusion.h" + +#include "utils.h" + +int main(int argc, char *argv[]) { + if(argc != 5) { + printf("Usage: %s [typeAlgo] [taille] [typeTableau] [isAfficher]\n", argv[0]); + exit(1); + } + + char typeAlgo = argv[1][0]; + long taille = (long) atoi(argv[2]); + long MAX = 50; + long tab[taille]; + char typeTable = argv[3][0]; + size_t afficher = (size_t) atoi(argv[4]); + + genTab(tab, taille, typeTable, MAX); + + switch(typeAlgo) { + case 'i': + triInsertion(tab,taille); + + if(afficher) { + printf("Tri Insertion: \n"); + affichertab(tab,taille); + } + + break; + + case 'r': + triRapide(tab, taille); + + if(afficher) { + printf("Tri Rapide: \n"); + affichertab(tab,taille); + } + + break; + + case 'f': + triFusion(tab, taille); + + if(afficher) { + printf("Tri Fusion: \n"); + affichertab(tab,taille); + } + break; + } + + return 0; +} diff --git a/Projet/test.R b/Projet/test.R new file mode 100644 index 0000000000000000000000000000000000000000..9d98a9dc31ba5217346a74bf9244d1667de9feb3 --- /dev/null +++ b/Projet/test.R @@ -0,0 +1,7 @@ +library(ggplot2) +perf <- read.table("test.dat", header = TRUE) +ggplot(perf, aes(x=taille, y=temps, group=algo, colour=as.character(algo))) + facet_grid(algo ~ typeTableau) + +geom_point() + geom_smooth() + + ggtitle("Graphes Comparaison Algos") + +ggsave("graphe.png") diff --git a/Projet/test.dat b/Projet/test.dat new file mode 100644 index 0000000000000000000000000000000000000000..682bb4ed2f134d73aab052214c15504b2d4eb4bd --- /dev/null +++ b/Projet/test.dat @@ -0,0 +1,121 @@ +-e iTest taille temps mem algo typeTableau +-e 1 59296 2.17 1772 i a +-e 1 59296 0.00 1704 i r +-e 1 59296 4.47 1656 i i +-e 1 59296 0.00 1700 i c +-e 1 59296 0.23 1848 r a +-e 1 59296 11.35 5416 r r +-e 1 59296 7.77 5412 r i +-e 1 59296 11.25 5412 r c +-e 1 59296 0.01 1992 f a +-e 1 59296 0.01 1964 f r +-e 1 59296 0.00 2032 f i +-e 1 59296 0.00 1956 f c +-e 2 97794 6.03 1988 i a +-e 2 97794 0.00 1988 i r +-e 2 97794 12.13 1928 i i +-e 2 97794 0.00 2012 i c +-e 2 97794 0.61 2116 r a +-e 2 97794 30.32 8036 r r +-e 2 97794 20.98 8116 r i +-e 2 97794 30.33 8100 r c +-e 2 97794 0.01 2604 f a +-e 2 97794 0.01 2544 f r +-e 2 97794 0.01 2680 f i +-e 2 97794 0.01 2556 f c +-e 3 41540 1.08 1496 i a +-e 3 41540 0.00 1576 i r +-e 3 41540 2.17 1456 i i +-e 3 41540 0.00 1628 i c +-e 3 41540 0.11 1628 r a +-e 3 41540 5.49 4080 r r +-e 3 41540 3.82 4152 r i +-e 3 41540 5.39 4224 r c +-e 3 41540 0.00 1908 f a +-e 3 41540 0.00 1892 f r +-e 3 41540 0.00 1836 f i +-e 3 41540 0.00 1972 f c +-e 4 97964 5.95 1936 i a +-e 4 97964 0.00 2040 i r +-e 4 97964 12.07 1992 i i +-e 4 97964 0.00 2048 i c +-e 4 97964 0.62 2124 r a +-e 4 97964 32.03 8192 r r +-e 4 97964 21.83 8080 r i +-e 4 97964 30.63 8024 r c +-e 4 97964 0.02 2560 f a +-e 4 97964 0.01 2564 f r +-e 4 97964 0.01 2544 f i +-e 4 97964 0.01 2492 f c +-e 5 39669 1.00 1544 i a +-e 5 39669 0.00 1496 i r +-e 5 39669 2.03 1504 i i +-e 5 39669 0.00 1536 i c +-e 5 39669 0.10 1552 r a +-e 5 39669 5.03 4032 r r +-e 5 39669 3.44 3984 r i +-e 5 39669 5.00 4024 r c +-e 5 39669 0.00 1792 f a +-e 5 39669 0.00 1820 f r +-e 5 39669 0.00 1840 f i +-e 5 39669 0.00 1884 f c +-e 6 8038 0.04 1300 i a +-e 6 8038 0.00 1300 i r +-e 6 8038 0.08 1220 i i +-e 6 8038 0.00 1288 i c +-e 6 8038 0.00 1300 r a +-e 6 8038 0.20 1756 r r +-e 6 8038 0.14 1812 r i +-e 6 8038 0.21 1808 r c +-e 6 8038 0.00 1448 f a +-e 6 8038 0.00 1448 f r +-e 6 8038 0.00 1332 f i +-e 6 8038 0.00 1364 f c +-e 7 30705 0.59 1468 i a +-e 7 30705 0.00 1524 i r +-e 7 30705 1.19 1480 i i +-e 7 30705 0.00 1492 i c +-e 7 30705 0.06 1472 r a +-e 7 30705 3.02 3440 r r +-e 7 30705 2.08 3408 r i +-e 7 30705 3.00 3384 r c +-e 7 30705 0.00 1688 f a +-e 7 30705 0.00 1688 f r +-e 7 30705 0.00 1616 f i +-e 7 30705 0.00 1696 f c +-e 8 53435 1.81 1660 i a +-e 8 53435 0.00 1700 i r +-e 8 53435 3.66 1656 i i +-e 8 53435 0.00 1640 i c +-e 8 53435 0.18 1676 r a +-e 8 53435 9.15 4892 r r +-e 8 53435 6.50 5000 r i +-e 8 53435 9.16 4928 r c +-e 8 53435 0.01 1996 f a +-e 8 53435 0.00 1904 f r +-e 8 53435 0.00 1988 f i +-e 8 53435 0.00 1992 f c +-e 9 50961 1.61 1676 i a +-e 9 50961 0.00 1640 i r +-e 9 50961 3.30 1624 i i +-e 9 50961 0.00 1592 i c +-e 9 50961 0.17 1716 r a +-e 9 50961 8.31 4888 r r +-e 9 50961 5.85 4832 r i +-e 9 50961 8.27 4852 r c +-e 9 50961 0.00 1860 f a +-e 9 50961 0.00 1928 f r +-e 9 50961 0.00 1884 f i +-e 9 50961 0.00 1884 f c +-e 10 74748 3.49 1832 i a +-e 10 74748 0.00 1828 i r +-e 10 74748 7.11 1744 i i +-e 10 74748 0.00 1772 i c +-e 10 74748 0.37 1932 r a +-e 10 74748 17.97 6456 r r +-e 10 74748 12.53 6388 r i +-e 10 74748 18.00 6496 r c +-e 10 74748 0.01 2360 f a +-e 10 74748 0.01 2424 f r +-e 10 74748 0.01 2380 f i +-e 10 74748 0.01 2360 f c diff --git a/Projet/test.sh b/Projet/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..027e8ba3efde7d1522c79da0626662c3b091cfd8 --- /dev/null +++ b/Projet/test.sh @@ -0,0 +1,21 @@ +#!/bin/bash + +affichage=0 +#taille=100 + +echo -e "iTest\ttaille\ttemps\tmem\talgo\ttypeTableau" + +for iTest in `seq 1 10` +do + taille=$(( `od -An -N4 -tu < /dev/urandom` % 100000)) + + for algo in 'i' 'r' 'f' + do + + for typeTableau in 'a' 'r' 'i' 'c' + do + res=`( /usr/bin/time -f "%U\t%M" ./main $algo $taille $typeTableau $affichage > /dev/null ) 2>&1` + echo -e "$iTest\t$taille\t$res\t$algo\t$typeTableau" + done + done +done diff --git a/Projet/tri_fusion.c b/Projet/tri_fusion.c new file mode 100644 index 0000000000000000000000000000000000000000..7453f701675217f5c498af12770c5557160a4909 --- /dev/null +++ b/Projet/tri_fusion.c @@ -0,0 +1,62 @@ +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include "tri_fusion.h" + +void triFusion(long* A, size_t size) { + sousTriFusion(A, 0, size); +} + +void sousTriFusion(long* A, size_t first, size_t size) { + if(first+1 <size){ // p < r - 1 + size_t middle = floor((first+size)/2); + sousTriFusion(A, first, middle); + sousTriFusion(A, middle, size); + fusion(A, first, middle, size); + } +} + +void copySousTable(long* mainT, long* underT, size_t id, size_t size) { + for(size_t i = id; i < id+size; ++i) { + underT[i-id] = mainT[i]; + } +} + +void fusion(long* A, size_t first, size_t middle, size_t size) { + size_t n1 = middle - first; // Nb elem dans A[p , q] q exclu + size_t n2 = size - middle; // Nb elem dans A[q , r] r exclu + + size_t indg = 0; + size_t indd = 0; + + long* ag = malloc(sizeof(long) * n1); + long* ad = malloc(sizeof(long) * n2); + + copySousTable(A, ag, first, n1); + copySousTable(A, ad, middle, n2); + + int i = first; + + while(i < size) { + if(indg == n1) { + A[i] = ad[indd]; + indd++; + } + else if(indd == n2) { + A[i] = ag[indg]; + indg++; + } + else if(ag[indg] < ad[indd]) { + A[i] = ag[indg]; + indg++; + } + else { + A[i] = ad[indd]; + indd++; + } + ++i; + } + + free(ag); + free(ad); +} \ No newline at end of file diff --git a/Projet/tri_fusion.h b/Projet/tri_fusion.h new file mode 100644 index 0000000000000000000000000000000000000000..093102cb4f5b15f86b7c7905363bd1f945d04424 --- /dev/null +++ b/Projet/tri_fusion.h @@ -0,0 +1,9 @@ +#ifndef TRIS_F +#define TRIS_F +#include <stdint.h> +#include <stdlib.h> + +void triFusion(long* A, size_t size); +void sousTriFusion(long* A, size_t first, size_t size); +void fusion(long* A, size_t first, size_t middle, size_t size); +#endif //TRIS_H \ No newline at end of file diff --git a/Projet/tri_insertion.c b/Projet/tri_insertion.c new file mode 100644 index 0000000000000000000000000000000000000000..b276ebd2d589e398fbd838f129fc3a6207dbef76 --- /dev/null +++ b/Projet/tri_insertion.c @@ -0,0 +1,16 @@ +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include "tri_insertion.h" + +void triInsertion(long* A, size_t n) { + for(size_t i = 1; i <= n-1; i++){ + long cle = A[i]; + size_t j = i - 1; + while(j+1 >= j && A[j] > cle){ + A[j+1] = A[j]; + j = j-1; + } + A[j+1] = cle; + } +} \ No newline at end of file diff --git a/Projet/tri_insertion.h b/Projet/tri_insertion.h new file mode 100644 index 0000000000000000000000000000000000000000..7e92151e6896f0e4c74d4986d79d20bae9595fba --- /dev/null +++ b/Projet/tri_insertion.h @@ -0,0 +1,7 @@ +#ifndef TRIS_I +#define TRIS_I +#include <stdint.h> +#include <stdlib.h> + +void triInsertion(long* A, size_t n); +#endif //TRIS_H \ No newline at end of file diff --git a/Projet/tri_rapide.c b/Projet/tri_rapide.c new file mode 100644 index 0000000000000000000000000000000000000000..41eaa3b77e77dd30d53a041ca5f88490cf06a16d --- /dev/null +++ b/Projet/tri_rapide.c @@ -0,0 +1,36 @@ +#include <math.h> +#include <stdlib.h> +#include <stdio.h> +#include "tri_rapide.h" + + +void triRapide(long* A, size_t n) { + sousTriRapide(A,0,n); +} + +void sousTriRapide(long* A, size_t first, size_t size){ + if(first+1 < size){ + size_t middle = partition(A,first,size); + sousTriRapide(A,first,middle); + sousTriRapide(A,middle+1,size); + } +} + +size_t partition(long* A, size_t first, size_t size) { + long pivot = A[size-1]; + size_t i = first; + for(int j = first; j+2 <= size; j++){ + if(A[j] <= pivot){ + permuter(A,i,j); + i++; + } + } + permuter(A,i,size-1); + return i; +} + +void permuter(long* A,size_t i,size_t j) { + long inter = A[i]; + A[i] = A[j]; + A[j] = inter; +} diff --git a/Projet/tri_rapide.h b/Projet/tri_rapide.h new file mode 100644 index 0000000000000000000000000000000000000000..e17099f3db77cc6284f5e220d7f930886672a500 --- /dev/null +++ b/Projet/tri_rapide.h @@ -0,0 +1,10 @@ +#ifndef TRIS_R +#define TRIS_R +#include <stdint.h> +#include <stdlib.h> + +void triRapide(long* A, size_t n); +void sousTriRapide(long* A, size_t first, size_t size); +size_t partition(long* A, size_t first, size_t size); +void permuter(long* A,size_t i,size_t j); +#endif //TRIS_H \ No newline at end of file diff --git a/Projet/utils.c b/Projet/utils.c new file mode 100644 index 0000000000000000000000000000000000000000..791c21c8fce431a5d5c7d2177d8033b7b70abda1 --- /dev/null +++ b/Projet/utils.c @@ -0,0 +1,55 @@ +#include <stdio.h> +#include <stdlib.h> +#include "utils.h" + +void affichertab(long *T, int s) +{ + printf("[ "); + for (int i = 0; i < s - 1; i++) + { + printf("%d , ", T[i]); + } + printf("%d ]\n", T[s - 1]); +} + +void genTab(long *T, long taille, char type, long MAX) +{ + + switch (type) + { + case 'a': + //Tableau Aleatoire + for (long i = 0; i < taille; ++i) + { + T[i] = rand() % MAX; + } + break; + + case 'r': + //Tableau Rangé + for (long i = 0; i < taille; ++i) + { + T[i] = i; + } + break; + + case 'i': + //Tableau Rangé Inversé + for (long i = 0; i < taille; ++i) + { + T[i] = taille - i; + } + break; + + case 'c': + //Tableau Constant + for (long i = 0; i < taille; ++i) + { + T[i] = 0; + } + break; + + default: + break; + } +} diff --git a/Projet/utils.h b/Projet/utils.h new file mode 100644 index 0000000000000000000000000000000000000000..15623a3956a537978db12072ac806e4c686b706e --- /dev/null +++ b/Projet/utils.h @@ -0,0 +1,5 @@ +#include <stdio.h> +#include <stdlib.h> + +void affichertab(long* T, int s); +void genTab(long *T, long taille, char type, long MAX);