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 (21)
Showing with 539 additions and 0 deletions
*.o
\ No newline at end of file
#include "algos.h"
#include <string.h>
void triInsertion(long* A, size_t n){
long cle = 0;
size_t max = 0; max--;
for(size_t i = 1; i<n; i++){
cle=A[i];
size_t j = i - 1;
while (j != max && A[j] > cle ){
A[j+1] = A[j];
j = j-1;
}
A[j+1] = cle;
}
}
void sousTriFusion(long * A, size_t p, size_t r){
if(p<(r-1)){
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, size_t p, size_t q, size_t r){
size_t n1 = q-p;
size_t n2 = r-q;
long Ad[n2];
memset(Ad, 0, n2);
int j =0;
for(size_t i = q; i<r; i++){
Ad[j] = A[i];
j++;
}
long Ag[n1];
memset(Ag, 0, n1);
j =0;
for(size_t i = p; i<q; i++){
Ag[j] = A[i];
j++;
}
size_t indg = 0;
size_t indd = 0;
size_t i = p;
while (i < r){
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++;
}
}
void triFusion(long * A, size_t n){
sousTriFusion(A, 0, n);
}
void sousTriRapide(long* A, size_t p, size_t r) {
size_t max = 0; max--;
if(r-1 != max){
if (p<(r-1)) {
size_t q = partition(A, p, r);
sousTriRapide(A, p, q);
sousTriRapide(A, q+1, r);
}
}
}
size_t partition(long* A, size_t p, size_t r) {
long pivot = A[r-1];
size_t i = p;
for (size_t j = p; j <= r-2; j++) {
if (A[j] <= pivot) {
long temp = A[i];
A[i] = A[j];
A[j] = temp;
i++;
}
}
long temp = A[i];
A[i] = A[r-1];
A[r-1] = temp;
return i;
}
void triRapide(long* A, size_t n) {
sousTriRapide(A, 0, n);
}
\ No newline at end of file
#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>
void triInsertion(long* A, size_t n);
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 triRapide(long* A, size_t n);
size_t partition(long* A, size_t p, size_t r);
void sousTriRapide(long* A, size_t p, size_t r);
File added
#include "triInsertion.h"
#include "triFusion.h"
#include "triRapide.h"
#include "utils.h"
int main(int argc, char **argv){
if(argc<3){
printf("Usage: ./tri <option> <input.txt> d \n");
exit(1);
}
long tab[100];
struct data data;
initData(&data);
if((strcmp(argv[1], "--insertion") == 0) || strcmp(argv[1], "-i")== 0){
int n = readToTab(tab, argv[2]);
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");
}
else if(strcmp(argv[1], "--fusion")== 0 || strcmp(argv[1], "-f")== 0){
int n = readToTab(tab, argv[2]);
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");
}
else if((strcmp(argv[1], "--rapide")== 0) || strcmp(argv[1], "-r")== 0){
int n = readToTab(tab, argv[2]);
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");
}
else if (strcmp(argv[1], "-a") == 0)
{
int n = readToTab(tab, argv[2]);
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");
}
else if (strcmp(argv[1], "-g") == 0)
{
if(argc<3){
printf("Usage: ./tri -g <output.txt> <size>\n");
exit(1);
}
long *tab;
size_t t = 0;
if(argc==3){
t = NULL;
}
else{
t = atoi(argv[3]);
}
size_t l = 0;
tab = generate_tab(t, &l);
for(int i = 0; i<t; 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 = readToTab(tab, argv[2]);
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);
}
else if((strcmp(argv[1], "--fusion-verbose") == 0) || strcmp(argv[1], "-fv")== 0){
int n = readToTab(tab, argv[2]);
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);
}
else if((strcmp(argv[1], "--rapide-verbose") == 0) || strcmp(argv[1], "-rv")== 0){
int n = readToTab(tab, argv[2]);
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);
}
else{
printf("Usage: ./tri <option> <input.txt> f\n");
}
return 0;
}
\ No newline at end of file
all: tri
tri: main.c triInsertion.o triFusion.o triRapide.o utils.o
gcc -Wall main.c triInsertion.o triFusion.o triRapide.o utils.o -Wall -o tri
insertion: triInsertion.c triInsertion.h
gcc -c triInsertion.c triInsertion.h -Wall
fusion: triFusion.c triFusion.h
gcc -c triFusion.c triFusion.h -Wall
rapide: triRapide.c triRapide.h
gcc -c triRapide.c triRapide.h -Wall
utils: utils.c utils.h
gcc -c utils.c utils.h -Wall
clear:
rm *.o
\ No newline at end of file
# Manuel
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<br>
- `-f` ou `--fusion` pour le tri à fusion<br>
- `-r` ou `--rapide` pour le tri rapide<br>
- `-a` ou `--all` pour tout les tris disponibles <br>
\ No newline at end of file
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
iTest taille tri temps mem
1 60 fusion 0.00 1468
1 60 insertion 0.00 1488
1 60 rapide 0.00 1524
2 9 fusion 0.00 1480
2 9 insertion 0.00 1492
2 9 rapide 0.00 1476
3 87 fusion 0.00 1448
3 87 insertion 0.00 1520
3 87 rapide 0.00 1504
#!/bin/bash
echo -e "iTest\ttaille\ttri\ttemps\tmem"
for iTest in `seq 1 3`
do
taille=$(( `od -An -N4 -tu < /dev/urandom` % 100))
for tri in "fusion" "insertion" "rapide"
do
`( ./tri -g $tri $taille ) > tabs/$tri.txt`
res=`( /usr/bin/time -f "%U\t%M" ./tri --$tri tabs/$tri.txt > /dev/null ) 2>&1`
echo -e "$iTest\t$taille\t$tri\t$res"
done
done
\ No newline at end of file
40 47 12 2 25 3 14 65 48 10 1 56 94 45 36 27 6 7 19 16 24 38 37 11 78 5 18 54 20 21 82 42 32 22 19 9 53 41 99 125 111 248 33 .
\ No newline at end of file
383 886 777 915 793 335 386 492 649 421 362 27 690 59 763 926 540 426 172 736 211 368 567 429 782 530 862 123 67 135 929 802 22 58 69 167 393 456 11 42 229 373 421 919 784 537 198 324 315 370 413 526 91 980 956 873 862 170 996 281 305 925 84 327 336 505 846 729 313 857 124 895 582 545 814 367 434 364 43 750 87 808 276 178 788 584 403 .
383 886 777 915 793 335 386 492 649 421 362 27 690 59 763 926 540 426 172 736 211 368 567 429 782 530 862 123 67 135 929 802 22 58 69 167 393 456 11 42 229 373 421 919 784 537 198 324 315 370 413 526 91 980 956 873 862 170 996 281 305 925 84 327 336 505 846 729 313 857 124 895 582 545 814 367 434 364 43 750 87 808 276 178 788 584 403 .
40 47 12 2 25 3 14 65 48 10 1 56 94 45 36 27 6 7 19 16 24 38 37 11 .
\ No newline at end of file
383 886 777 915 793 335 386 492 649 421 362 27 690 59 763 926 540 426 172 736 211 368 567 429 782 530 862 123 67 135 929 802 22 58 69 167 393 456 11 42 229 373 421 919 784 537 198 324 315 370 413 526 91 980 956 873 862 170 996 281 305 925 84 327 336 505 846 729 313 857 124 895 582 545 814 367 434 364 43 750 87 808 276 178 788 584 403 .
3 4 5 6 1 2 9 11 .
\ No newline at end of file
File added
#include "triFusion.h"
void sousTriFusion(long * A, size_t p, size_t r){
if(p<(r-1)){
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, size_t p, size_t q, size_t r){
size_t n1 = q-p;
size_t n2 = r-q;
long Ad[n2];
memset(Ad, 0, n2);
int j =0;
for(size_t i = q; i<r; i++){
Ad[j] = A[i];
j++;
}
long Ag[n1];
memset(Ag, 0, n1);
j =0;
for(size_t i = p; i<q; i++){
Ag[j] = A[i];
j++;
}
size_t indg = 0;
size_t indd = 0;
size_t i = p;
while (i < r){
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++;
}
}
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 "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
File added