Compare commits
2 Commits
Author | SHA1 | Date | |
---|---|---|---|
6f57fcabff | |||
97575ac067 |
78
Algo_ex3.c
Normal file
78
Algo_ex3.c
Normal file
@@ -0,0 +1,78 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
static size_t ipow(size_t a, unsigned b) {
|
||||
size_t r = 1; while (b--) r *= a; return r;
|
||||
}
|
||||
|
||||
static int cmp_int(const void *a, const void *b) {
|
||||
int ia = *(const int*)a, ib = *(const int*)b;
|
||||
return (ia > ib) - (ia < ib);
|
||||
}
|
||||
|
||||
|
||||
static void reorder_blocks(int *base, size_t block_sz, size_t M,const size_t *idx)
|
||||
{
|
||||
size_t group_sz = block_sz * M;
|
||||
int *tmp = malloc(group_sz * sizeof(int));
|
||||
for (size_t j = 0; j < M; ++j)
|
||||
memcpy(tmp + j*block_sz, base + idx[j]*block_sz, block_sz*sizeof(int));
|
||||
memcpy(base, tmp, group_sz * sizeof(int));
|
||||
free(tmp);
|
||||
}
|
||||
|
||||
|
||||
void sort_ND(int *a, unsigned N, size_t M)
|
||||
{
|
||||
const size_t total = ipow(M, N);
|
||||
|
||||
|
||||
for (size_t off = 0; off < total; off += M)
|
||||
qsort(a + off, M, sizeof(int), cmp_int);
|
||||
|
||||
for (unsigned level = 2; level <= N; ++level) {
|
||||
size_t block_sz = ipow(M, level - 1);
|
||||
size_t group_sz = block_sz * M;
|
||||
size_t groups = total / group_sz;
|
||||
|
||||
long *sums = malloc(M * sizeof(long));
|
||||
size_t *idx = malloc(M * sizeof(size_t));
|
||||
|
||||
for (size_t g = 0; g < groups; ++g) {
|
||||
int *base = a + g * group_sz;
|
||||
|
||||
|
||||
for (size_t j = 0; j < M; ++j) {
|
||||
long s = 0;
|
||||
int *p = base + j * block_sz;
|
||||
for (size_t t = 0; t < block_sz; ++t) s += p[t];
|
||||
sums[j] = s;
|
||||
idx[j] = j;
|
||||
}
|
||||
|
||||
for (size_t i = 0; i + 1 < M; ++i)
|
||||
for (size_t j = i + 1; j < M; ++j)
|
||||
if (sums[idx[j]] < sums[idx[i]]) {
|
||||
size_t tmp = idx[i]; idx[i] = idx[j]; idx[j] = tmp;
|
||||
}
|
||||
|
||||
|
||||
reorder_blocks(base, block_sz, M, idx);
|
||||
}
|
||||
free(sums); free(idx);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
int main(void) {
|
||||
size_t M = 3; unsigned N = 2;
|
||||
int a[] = { 0,3,2, 9,4,5, 4,1,3 };
|
||||
sort_ND(a, N, M);
|
||||
|
||||
for (size_t i = 0; i < M; ++i) {
|
||||
for (size_t j = 0; j < M; ++j) printf("%d ", a[i*M + j]);
|
||||
printf("\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
16
EX2.txt
Normal file
16
EX2.txt
Normal file
@@ -0,0 +1,16 @@
|
||||
1)
|
||||
pour function_1 :
|
||||
on a deux tableaux qu'on parcour en entier en comparant les elements un a un qui ont le meme indice ( tab[n] == tab[m] ? )
|
||||
On a donc O(n*m), ou n represente le premier tableau et m le deuxieme
|
||||
|
||||
2)
|
||||
pour function_2 :
|
||||
La boucle while s'execute autant de fois que la valeur initiale de x (si x>0). On a donc un temps proportionnel a x.
|
||||
On a donc O(x) comme complexite
|
||||
|
||||
|
||||
3)
|
||||
pour function_3 :
|
||||
Un nombre fixe de tests if et d'affectations, independants de la taille de l'entre comme dans l'exemple dans le cours.
|
||||
On va faire qu'une seule instruction quoi qui l'arrive. Selon la valeur de x on va faire une action differente mais dans tous les cas il ni y'en aura qu'une seule.
|
||||
La complexite est donc de O(1)
|
20
EX3.txt
Normal file
20
EX3.txt
Normal file
@@ -0,0 +1,20 @@
|
||||
A chaque niveau, on effectue des tris de taille M sur un certain nombre de tranches/blocs
|
||||
|
||||
|
||||
Role de chaque fonction
|
||||
|
||||
ipow(a, b) : calcule a^b par multiplications successives.
|
||||
|
||||
cmp_int(a, b) : comparateur d'entiers pour qsort (retourne l'ordre).
|
||||
|
||||
reorder_blocks(base, block_sz, M, idx) : reordonne, dans un groupe de M blocs contigus de taille block_sz,
|
||||
l'ordre des blocs selon le tableau d'indices idx (via un tampon temporaire).
|
||||
|
||||
sort_ND(a, N, M) :
|
||||
- trie chaque sous-tableau 1D de longueur M (dimension la plus basse)
|
||||
|
||||
pour chaque niveau 2...N, calcule la somme de chaque bloc de la dimension inferieure, trie les blocs selon ces sommes, puis rerdonne physiquement les blocs.
|
||||
|
||||
Etant donne que notre parcours se fait sur le nombre d'element a parcourir dans chaque dimensions (c'est M), et que ce parcour on doit le faire sur chaque dimensions
|
||||
On a donc M^N (M puissance N) comme complexite algorithmique.
|
||||
|
Reference in New Issue
Block a user