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 (45)
Showing with 2314 additions and 6 deletions
......@@ -14,10 +14,22 @@ Description de tous les paramètres exploratoires du problème
### Application
[code source de l'application](chemin)
```
Description de l'application et des arguments
```
[code source de l'application](src/Lib)
>Pour utiliser les tris, vous avez besoin de 2 choses:<br>
- Le binaire `tri`<br>
- Un document texte contenant les éléments du tableau, chaque élément est séparé du prochain par un espace et pour finit par un `.`. <br>
Ex : `4 3 11 29 .`<br>
> Le binaire demande une option et un fichier, le fichier est du type de ceux définit plus haut, quant aux option il en existe plusieures : <br>
- `-i` ou `--insertion` pour le tri à insertion
- `-f` ou `--fusion` pour le tri à fusion
- `-r` ou `--rapide` pour le tri rapide
- `-a` ou `--all` pour tout les tris disponibles
- `-iv` ou `--insertion-verbose` pour le tri à insertion avec les affichages avancés
- `-fv` ou `--fusion-verbose` pour le tri à fusion avec les affichages avancés
- `-rv` ou `--rapide-verbose` pour le tri rapide avec les affichages avancés
- `-g` qui génére un nouveau tableau sur stdout avec une taille du tableau et un nombre max
### Environnement de test
......@@ -65,15 +77,18 @@ préalables et ce qu'ils ne permettent pas de vérifier.
### Hypothèse
Expression précise et succincte d'une hypothèse.
Le tri insertion devient de plus en plus long quand la taille du tableau augmente, et donc celui-ci ne sait traiter efficacement uniquement des petits tableaux, étant donnée que tout les autres algorithmes coupent leurs tableaux en de plus petit tableaux, Notre hypothése est, que si le tri insertion est donc plus performant que les autres tris sur des petit tableaux, alors nous allons utiliser le tri insertion dans les autres tris, par exemple avec tri rapide.
### Protocole expérimental de vérification de l'hypothèse
Expression précise et succincte du protocole.
Pour ce faire nous allons donc voir si effectivement le tri insertion se déroule plus rapidement sur plein de petit tableaux, pour le test nous avons commencé avec des tableaux de taille 100, et au lieux de regarder le temps de chaque tri de faire le tri d'un tableau puis d'un autre, etc..., on va juste prendre le temps pour trier tout les tableaux.
En faisant certains test, nous avons aussi remarqué que le tri insertion est encore plus rapide lorsque les tableaux à traiter sont déjà presque triés
Tout d'abord pour tester l'hypothése on fait un petit script pour tester
```
Suite des commandes, ou script, à exécuter pour produire les données.
```
ce qui nous donne ![ceci](src/Images/Tri_insertion_plus_rapide.png)
### Résultats expérimentaux
......
#!/usr/bin/python
from sys import argv
from math import *
def Helloworld1(n):
count = 0
for i in range(0,n):
for j in range(0, n):
print("Helloworld")
count += 1
return count
def Helloworld2(n):
count = 0
for i in range(0,n):
for j in range(i, n):
print("Helloworld")
count +=1
return count
def Helloworld3(n):
count = 0
if(n>1):
Helloworld3(n/2)
Helloworld3(n-(n/2))
count+=1
print("Helloworld")
return count
def version(i, n):
if(i==1):
l = Helloworld1(n)
print(l)
elif(i==2):
l=Helloworld2(n)
print(l)
elif(i==3):
l=Helloworld3(n)
# print(l)
else:
print("non")
res = log(n)
print("log count : ", res)
print("nb : ", n*res)
if(len(argv[1:])):
versio = int(argv[1])
nb = int(argv[2])
version(versio,nb)
\ No newline at end of file
op = [10, 100, 1000, 10000, 100000, 1000000]
aps = 1000
for i in op:
print("--------------", i, "---------")
print("n : ", i/aps , "s")
print("ln(n) : ", log10(i)/aps , "s")
print("n.log(n) : ", (i*(log10(i)))/aps , "s")
print("nRacine(n) : ", sqrt(i)/aps , "s")
print("n² : ", (pow(i,2))/aps , "s")
print("n³ : ", (pow(i,3))/aps/3600/24 , "jours")
if(i < 10000):
print("2^n : ", (((((pow(2,i))/aps)/3600)/24)/365)/1000000000000000000 , "Quatrillion années")
if(i <1000):
print("fact(n) : ", (((((factorial(i)/aps)/3600)/24)/365)/1000000000000000000) , "Quatrillion années" )
*.o
tri
\ No newline at end of file
src/Images/Tri_insertion_plus_rapide.png

19.7 KiB

src/Images/graphe_temps.png

102 KiB

#include "../Tris/triInsertion.h"
#include "../Tris/triFusion.h"
#include "../Tris/triRapide.h"
#include "../Tris/triBulle.h"
#include "../Lib/utils.h"
int main(int argc, char **argv){
if(argc<3){
printf("Usage: ./tri <option> <input.txt> d \n");
exit(1);
}
struct data data;
initData(&data);
if((strcmp(argv[1], "--insertion") == 0) || strcmp(argv[1], "-i")== 0){
int n= 0;
long *tab = readToTab(argv[2], &n);
// printf("n:%d\n", n);
// printf("Base : ");
// for(int i = 0; i<n; i++){
// printf("%ld-", tab[i]);
// }
// printf("\n");
triInsertion(tab, n);
// printf("Insertion : ");
// for(int i = 0; i<n; i++){
// printf("%ld-", tab[i]);
// }
// printf("\n");
free(tab);
}
else if(strcmp(argv[1], "--fusion")== 0 || strcmp(argv[1], "-f")== 0){
int n= 0;
long *tab = readToTab(argv[2], &n);
// printf("Base : ");
// for(int i = 0; i<n; i++){
// printf("%ld,", tab[i]);
// }
// printf("\n");
triFusion(tab, n);
// printf("Fusion : ");
// for(int i = 0; i<n; i++){
// printf("%ld,", tab[i]);
// }
// printf("\n");
free(tab);
}
else if((strcmp(argv[1], "--rapide")== 0) || strcmp(argv[1], "-r")== 0){
int n= 0;
long *tab = readToTab(argv[2], &n);
// printf("Base : ");
// for(int i = 0; i<n; i++){
// printf("%ld,", tab[i]);
// }
// printf("\n");
triRapide(tab, n);
// printf("Rapide : ");
// for(int i = 0; i<n; i++){
// printf("%ld,", tab[i]);
// }
// printf("\n");
free(tab);
}
else if((strcmp(argv[1], "--bulle")== 0) || strcmp(argv[1], "-b")== 0){
int n= 0;
long *tab = readToTab(argv[2], &n);
// printf("Base : ");
// for(int i = 0; i<n; i++){
// printf("%ld,", tab[i]);
// }
// printf("\n");
triBulle(tab, n);
// printf("Bulle : ");
// for(int i = 0; i<n; i++){
// printf("%ld,", tab[i]);
// }
// printf("\n");
free(tab);
}
else if (strcmp(argv[1], "-a") == 0)
{
int n= 0;
long *tab = readToTab(argv[2], &n);
printf("Base : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
triInsertion(tab, n);
printf("Insertion : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
triFusion(tab, n);
printf("Fusion : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
triRapide(tab, n);
printf("Rapide : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
free(tab);
}
else if (strcmp(argv[1], "-g") == 0)
{
if(argc<3){
printf("Usage: ./tri -g <max> <size>\n");
exit(1);
}
size_t max = atoi(argv[2]);
size_t size = atoi(argv[3]);
long* tab = generate_tab(max, size);
for(size_t i = 0; i<size; i++){
printf("%ld ", tab[i]);
}
printf(".");
printf("\n");
// free(tab);
}
else if((strcmp(argv[1], "--insertion-verbose") == 0) || strcmp(argv[1], "-iv")== 0){
int n= 0;
long *tab = readToTab(argv[2], &n);
printf("Base : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
data.time = clock();
triInsertionVerbose(tab, n, &data);
long new_time = (double)clock() - data.time;
data.time = new_time;
printf("Insertion : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
printData(data);
free(tab);
}
else if((strcmp(argv[1], "--fusion-verbose") == 0) || strcmp(argv[1], "-fv")== 0){
int n= 0;
long *tab = readToTab(argv[2], &n);
printf("Base : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
data.time = clock();
triFusionVerbose(tab, n, &data);
long new_time = (double)clock() - data.time;
data.time = new_time;
printf("Fusion : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
printData(data);
free(tab);
}
else if((strcmp(argv[1], "--rapide-verbose") == 0) || strcmp(argv[1], "-rv")== 0){
int n= 0;
long *tab = readToTab(argv[2], &n);
printf("Base : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
data.time = clock();
triInsertionVerbose(tab, n, &data);
long new_time = (double)clock() - data.time;
data.time = new_time;
printf("Rapide : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
printData(data);
free(tab);
}
else{
printf("Usage: ./tri <option> <input.txt> f\n");
}
return 0;
}
\ No newline at end of file
#include "utils.h"
long* readToTab(char* file, int *sizze){
int fd = open(file, O_RDONLY);
if(fd == -1){
perror("open");
exit(1);
}
int n = 0;
int sizet = 1000, nb_size = 1;
int nb_size_tab = 1;
size_t x=0;
char temp;
char* tmp = malloc(sizeof(char)*sizet);
memset(tmp, 0, sizet);
read(fd,&temp,sizeof(char));
int size_tab = 0;
while(temp != '.'){
if(temp == ' '){
size_tab++;
}
read(fd,&temp,sizeof(char));
}
*sizze = size_tab;
lseek(fd, 0, SEEK_SET);
long *tab = malloc(sizeof(long)*size_tab);
read(fd,&temp,sizeof(char));
while(temp != '.'){
// printf("n:%d j:%d nb_size:%d\n", n,j,nb_size);
if(temp != ' '){
tmp[x] = temp;
//printf("tmp[%d] = %c\n", x, temp);
x++;
}
else{
long t = atol(tmp);
tab[n] = t;
//printf("char:%c long:%d n:%d j:%d nb_size:%d, size:%d\n",temp,tab[j], n,j,nb_size, sizet);
memset(tmp, 0, sizet*nb_size);
x=0;
n++;
}
read(fd,&temp,sizeof(char));
}
// for(int i = 0; i<size_tab; i++){
// if(i%15 == 0)
// printf("i:%ld\n",tab[i]);
// else
// printf("i:%ld ", tab[i]);
// }
free(tmp);
return tab;
}
void initData(struct data* d){
d->time = 0;
d->comparison = 0;
d->ct_fusion = 0;
d->ct_parse = 0;
d->write = 0;
}
long* generate_tab(size_t max, size_t length){
if(length==0){
return 0;
}
long* tab = malloc(sizeof(long)*length+1);
memset(tab, 0, sizeof(long)*length+1);
size_t index = 0;
for(index = 0; index<length; index++){
long p = (long)(rand()%max);
tab[index] = p;
}
return tab;
}
void printData(struct data d){
printf("Time passed : %ld\n", d.time);
printf("Number of comparison : %ld\n", d.comparison);
printf("Number of write : %ld\n", d.write);
printf("Call of fusion : %ld\n", d.ct_fusion);
printf("Call of parsing : %ld\n", d.ct_parse);
}
\ No newline at end of file
#ifndef utils_h
#define utils_h
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <time.h>
#define TAB_SIZE 1000
struct data {
long time;
unsigned int write;
unsigned int comparison;
unsigned int ct_fusion;
unsigned int ct_parse;
};
long* readToTab(char* file, int *sizze);
void initData(struct data* d);
long* generate_tab(size_t max, size_t length);
void printData(struct data d);
#endif
library(ggplot2)
perf <- read.table("perf.dat", header = TRUE)
ggplot(perf, aes(x=taille, y=temps, group=tri, colour=as.character(tri))) + geom_point() +
geom_smooth() + ggtitle("Notre super graphe")
ggsave("graphe.png")
\ No newline at end of file
#!/bin/bash
for i in "insertion" "fusion" "rapide"
do
cmd=$(for j in `seq 1 1000`
do
../tri -g 10 100 1>../tabs/hypo_tab.txt
../tri --$i ../tabs/hypo_tab.txt
done )
time $cmd
done
\ No newline at end of file
iTest taille tri temps mem
1 78 fusion 0.00 1592
1 78 insertion 0.00 1512
1 78 rapide 0.00 1508
2 40 fusion 0.00 1416
2 40 insertion 0.00 1488
2 40 rapide 0.00 1524
3 56 fusion 0.00 1532
3 56 insertion 0.00 1460
3 56 rapide 0.00 1460
4 21 fusion 0.00 1472
4 21 insertion 0.00 1496
4 21 rapide 0.00 1476
5 12 fusion 0.00 1492
5 12 insertion 0.00 1520
5 12 rapide 0.00 1512
6 15 fusion 0.00 1516
6 15 insertion 0.00 1468
6 15 rapide 0.00 1468
7 92 fusion 0.00 1496
7 92 insertion 0.00 1508
7 92 rapide 0.00 1528
8 68 fusion 0.00 1592
8 68 insertion 0.00 1456
8 68 rapide 0.00 1488
9 60 fusion 0.00 1488
9 60 insertion 0.00 1512
9 60 rapide 0.00 1508
10 66 fusion 0.00 1456
10 66 insertion 0.00 1528
10 66 rapide 0.00 1472
#!/bin/bash
echo -e "iTest\ttaille\ttri\ttemps\tmem"
for iTest in `seq 1 3`
do
size=$(( `od -An -N4 -tu < /dev/urandom` % 100000))
<<<<<<< HEAD
max=$(( `od -An -N4 -tu < /dev/urandom` % 1000))
../tri -g $max $size 1>../tabs/perf_tab.txt
#cat tabs/perf_tab.txt
=======
max=$(( `od -An -N4 -tu < /dev/urandom` % 100000))
../tri -g $max $size 1 >../tabs/perf_tab.txt
>>>>>>> 844a1820ad7242804acf3e13046b619e62101ce6
for tri in "insertion" "fusion" "rapide" "bulle"
do
res=`( time -f "%U\t%M" ../tri --$tri ../tabs/perf_tab.txt > /dev/null ) 2>&1`
echo -e "$iTest\t$size\t$tri\t$res"
done
rm ../tabs/perf_tab.txt
done
\ No newline at end of file
#!/bin/bash
echo "iTest\ttaille\ttri\ttemps\tmem"
for iTest in `seq 1 3`
do
size=$(( `od -An -N4 -tu < /dev/urandom` % 100000))
max=$(( `od -An -N4 -tu < /dev/urandom` % 100000))
../tri -g $max $size 1 >../tabs/perf_tab.txt
for tri in "insertion" "fusion" "rapide" "bulle"
do
res=`( /usr/bin/time -f "%U\t%M" ../tri --$tri ../tabs/perf_tab.txt > /dev/null ) 2>&1`
echo "$iTest\t$size\t$tri\t$res"
done
rm ../tabs/perf_tab.txt
done
\ No newline at end of file
This diff is collapsed.
#include "triBulle.h"
void triBulle(long* A, size_t n){
for(int I = n - 2;I >= 0; I--) {
for(int J = 0; J <= I; J++) {
if(A[J + 1] < A[J]) {
int t = A[J + 1];
A[J + 1] = A[J];
A[J] = t;
}
}
}
}
\ No newline at end of file
#ifndef triBulle_h
#define triBulle_h
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "../Lib/utils.h"
void triBulle(long* A, size_t n);
#endif
\ No newline at end of file
#include "triFusion.h"
void sousTriFusion(long* A, const size_t p, const size_t r) {
if (p + 1 < r) {
size_t q = (size_t)((p + r) / 2);
sousTriFusion(A, p, q);
sousTriFusion(A, q, r);
fusion(A, p, q, r);
}
}
void fusion(long* A, const size_t p, const size_t q, const size_t r) {
size_t n1 = q - p;
size_t n2 = r - q;
long* Ag = (long*) malloc(n1 * sizeof(long));
long* Ad = (long*) malloc(n2 * sizeof(long));
size_t cursor = 0;
for (size_t i = p; i < q; i++) {
Ag[cursor++] = A[i];
}
cursor = 0;
for (size_t i = q; i < r; i++) {
Ad[cursor++] = A[i];
}
size_t ind_g = 0;
size_t ind_d = 0;
size_t i = p;
while (i < r) {
if (ind_g == n1) {
A[i] = Ad[ind_d++];
}
else if (ind_d == n2) {
A[i] = Ag[ind_g++];
}
else if (Ag[ind_g] < Ad[ind_d]) {
A[i] = Ag[ind_g++];
}
else {
A[i] = Ad[ind_d++];
}
i++;
}
}
void triFusion(long * A, size_t n){
sousTriFusion(A, 0, n);
}
// --------------------------------------------------------
void sousTriFusionVerbose(long * A, size_t p, size_t r, struct data* d){
if(p<(r-1)){
d->comparison++;
size_t q = (size_t)((p+r)/2);
d->write++;
sousTriFusionVerbose(A, p, q,d);
sousTriFusionVerbose(A, q, r, d);
fusionVerbose(A, p, q, r, d);
}
}
void fusionVerbose(long * A, size_t p, size_t q, size_t r, struct data* d){
size_t n1 = q-p;
d->write++;
size_t n2 = r-q;
d->write++;
long Ad[n2];
memset(Ad, 0, n2);
d->write+=n2;
int j = 0;
for(size_t i = q; i<r; i++){
Ad[j] = A[i];
d->write++;
j++;
d->write++;
}
long Ag[n1];
memset(Ag, 0, n1);
d->write+=n1;
j = 0;
for(size_t i = p; i<q; i++){
Ag[j] = A[i];
d->write++;
j++;
d->write++;
}
size_t indg = 0;
size_t indd = 0;
size_t i = p;
d->write++;
while (i < r){
d->comparison++;
if(indg == n1){
d->comparison++;
A[i] = Ad[indd];
d->write++;
indd++;
d->write++;
}
else if(indd == n2){
d->comparison+=2;
A[i] == Ag[indg];
d->write++;
indg++;
d->write++;
}
else if(Ag[indg] < Ad[indd]){
d->comparison+=3;
A[i] = Ag[indg];
d->write++;
indg++;
d->write++;
}
else{
d->comparison+=4;
A[i] = Ad[indd];
d->write++;
indd++;
d->write++;
}
i++;
d->write++;
}
}
void triFusionVerbose(long * A, size_t n, struct data* d){
sousTriFusionVerbose(A, 0, n, d);
}
\ No newline at end of file
#ifndef triFusion_h
#define triFusion_h
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include "../Lib/utils.h"
void sousTriFusion(long * A, size_t p, size_t r);
void fusion(long * A, size_t p, size_t q, size_t r);
void triFusion(long * A, size_t n);
void sousTriFusionVerbose(long * A, size_t p, size_t r, struct data* d);
void fusionVerbose(long * A, size_t p, size_t q, size_t r, struct data* d);
void triFusionVerbose(long * A, size_t n, struct data* d);
#endif
\ No newline at end of file
#include "triInsertion.h"
void triInsertion(long* A, size_t n){
long cle = 0;
for(size_t i = 1; i<n; i++){
cle=A[i];
size_t j = i - 1;
while (j+1 > 0 && A[j] > cle ){
A[j+1] = A[j];
j = j-1;
}
A[j+1] = cle;
}
}
// --------------------------------------------------------
void triInsertionVerbose(long* A, size_t n, struct data* d){
long cle = 0;
d->write++;
for(size_t i = 1; i<n; i++){
cle=A[i];
d->write++;
size_t j = i - 1;
d->write++;
while (j+1 > 0 && A[j] > cle ){
d->comparison+=2;
A[j+1] = A[j];
d->write++;
j = j-1;
d->write++;
}
A[j+1] = cle;
d->write++;
}
}
\ No newline at end of file