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 (27)
Showing with 572 additions and 0 deletions
*.o
tri
\ 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
383 886 777 915 793 .
383 886 777 915 793 .
Algos_1/graphe.png

63.6 KiB

#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 <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 = 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 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 2`
do
size=$(( `od -An -N4 -tu < /dev/urandom` % 100))
max=$(( `od -An -N4 -tu < /dev/urandom` % 1000))
./tri -g $max $size 1>tabs/perf_tab.txt
#cat tabs/perf_tab.txt
for tri in "fusion" "insertion" "rapide"
do
res=`( time "%U\t%M" ./tri --$tri tabs/perf_tab.txt > /dev/null ) 2>&1`
echo -e "$iTest\t$taille\t$tri\t$res"
done
rm tabs/perf_tab.txt
done
\ No newline at end of file
#!/bin/bash
echo -e "iTest\ttaille\ttri\ttemps\tmem"
for iTest in `seq 1 2`
do
size=$(( `od -An -N4 -tu < /dev/urandom` % 1000000000))
max=$(( `od -An -N4 -tu < /dev/urandom` % 1000))
./tri -g $max $size 1>tabs/perf_tab.txt
#cat tabs/perf_tab.txt
for tri in "fusion" "insertion" "rapide"
do
res=$($(time ./tri --$tri tabs/perf_tab.txt 1>/dev/null )2>&1)
echo -e "$iTest\t$taille\t$tri\t$res"
done
rm tabs/perf_tab.txt
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
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
This diff is collapsed.
#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