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 (9)
#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 "algos.h"
int readToTab(long* tab, char* file){
int n = 0;
int fd = open(file, O_RDONLY);
if(fd == -1){
perror("open");
exit(1);
}
char tmp[1000];
memset(tmp, 0, 1000);
char temp;
size_t j = 0, x=0;
read(fd,&temp,sizeof(char));
while(temp != '.'){
if(temp != ' '){
tmp[x] = temp;
x++;
}
else if(temp == ' '){
long t = atol(tmp);
tab[j] = t;
memset(tmp, 0, 1000);
j++;
x=0;
n++;
}
read(fd,&temp,sizeof(char));
}
return n-1;
}
int main(int argc, char **argv){
if(argc!=3){
printf("Usage: ./tri <option> <input.txt>\n");
exit(1);
}
long tab[100];
int n = readToTab(tab, argv[2]);
printf("Base : ");
for(int i = 0; i<n; i++){
printf("%ld,", tab[i]);
}
printf("\n");
if((strcmp(argv[1], "--insertion") == 0) || strcmp(argv[1], "-i")== 0){
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){
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){
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)
{
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{
printf("Usage: ./tri <option> <input.txt>\n");
}
return 0;
}
\ No newline at end of file
all: tri
tri: main.c algos.o
gcc -Wall main.c algos.o -Wall -o tri
algos: algos.c algos.h
gcc -c algos.c algos.h -Wall
clear:
rm *.o
\ No newline at end of file
#Manuel
Pour utiliser les tris, vous avez besoin de 2 choses:
- Le binaire `tri`
- 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 `.`,
Ex : `4 3 11 29 .`
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 :
- `-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
\ 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
3 4 5 6 1 2 9 11 .
\ No newline at end of file
File added