Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
No results found
Show changes
Commits on Source (28)
{
"files.associations": {
"stdio.h": "c"
}
}
\ No newline at end of file
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
Projet/ggossaprodansR.png

61.7 KiB

#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;
}
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")
-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
#!/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
#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
#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
#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
#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
#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;
}
#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
#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;
}
}
#include <stdio.h>
#include <stdlib.h>
void affichertab(long* T, int s);
void genTab(long *T, long taille, char type, long MAX);
......@@ -4,6 +4,8 @@
[Grille d'évaluation P4z](https://docs.google.com/spreadsheets/d/1VXeO91rhy04xa0p8KUhWliFl228utHaDir8MstO5Z-M/edit?usp=sharing
)
![plot](/TP2/testInsertion.png)
## Problème
Description du Problème.
......
all: main
main : tris.o utils.o main.c
gcc -o main -Wall utils.o tris.o -g main.c
tris.o : tris.c tris.h
gcc -c -Wall -g tris.c
utils.o : utils.c utils.h
gcc -o utils.o -c -g utils.c
clean :
rm *.o
File added
#include <stdio.h>
#include <stdlib.h>
#include "tris.h"
#include "utils.h"
int main() {
printf("Tri Insertion: \n");
long* triIns = listeAlea(100,300);
affichertab(triIns,100);
triInsertion(triIns,100);
affichertab(triIns,100);
printf("Tri Rapide: \n");
long* triRap = listeAlea(100,300);
affichertab(triRap,100);
triRapide(triRap, 100);
affichertab(triRap,100);
printf("Tri Fusion: \n");
long* triFus = listeAlea(100,300);
affichertab(triFus,100);
triFusion(triFus, 100);
affichertab(triFus,100);
return 0;
}
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include "tris.h"
void triInsertionDec(long* A, size_t n) {
for(size_t i = 1; i <= n-1; i++){
long cle = A[i];
size_t j = i - 1;
size_t ecriture=0;
while(j+1 >= j && A[j] > cle){
ecriture++;
A[j+1] = A[j];
j = j-1;
}
ecriture++;
A[j+1] = cle;
}
}
void triFusionDec(long* A, size_t size) {
sousTriFusionDec(A, 0, size);
}
void sousTriFusionDec(long* A, size_t first, size_t size) {
if(first+1 <size){ // p < r - 1
size_t middle = floor((first+size)/2);
sousTriFusionDec(A, first, middle);
sousTriFusionDec(A, middle, size);
fusionDec(A, first, middle, size);
}
}
void copySousTableDec(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 fusionDec(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);
copySousTableDec(A, ag, first, n1);
copySousTableDec(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);
}
void triRapideDec(long* A, size_t n) {
sousTriRapideDec(A,0,n);
}
void sousTriRapideDec(long* A, size_t first, size_t size){
if(first+1 < size){
size_t middle = partitionDec(A,first,size);
sousTriRapideDec(A,first,middle);
sousTriRapideDec(A,middle+1,size);
}
}
size_t partitionDec(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){
permuterDec(A,i,j);
i++;
}
}
permuterDec(A,i,size-1);
return i;
}
void permuterDec(long* A,size_t i,size_t j) {
long inter = A[i];
A[i] = A[j];
A[j] = inter;
}