diff options
| author | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-02-21 17:50:16 +0100 |
|---|---|---|
| committer | Anhgelus Morhtuuzh <anhgelus@anhgelus.world> | 2025-02-21 17:50:25 +0100 |
| commit | 77bfb2ccd3152c1f41d43dc192ba86ca8fd0f72f (patch) | |
| tree | 798ab77b1c1608ef8cc6e56f3d12778c0844b03b /semestre 2/informatique/tme/semaine4/24_insertion.c | |
| parent | a1a5447b8b040b100bad89766066ae4ba8d6d920 (diff) | |
Ajout de la semaine des cours du 14 au 21 février
Diffstat (limited to 'semestre 2/informatique/tme/semaine4/24_insertion.c')
| -rw-r--r-- | semestre 2/informatique/tme/semaine4/24_insertion.c | 41 |
1 files changed, 41 insertions, 0 deletions
diff --git a/semestre 2/informatique/tme/semaine4/24_insertion.c b/semestre 2/informatique/tme/semaine4/24_insertion.c new file mode 100644 index 0000000..4f44f90 --- /dev/null +++ b/semestre 2/informatique/tme/semaine4/24_insertion.c @@ -0,0 +1,41 @@ +#include <stdio.h> + +int indiceInsert(int *tab, int taille, int nbEl, int el){ + if (nbEl >= taille) return -1; + int i = 0; + while (i < nbEl && tab[i] <= el) { + if (tab[i] == el) return -1; + i++; + } + return i; +} + +int insertElt(int el, int *tab, int taille, int *nb) { + int ind = indiceInsert(tab, taille, *nb, el); + if (ind == -1) return 0; + int prev = 0; + int set = 0; + for (int i = ind; i < *nb+1; i++){ + if (set == 0) { + set = 1; + prev = tab[i]; + tab[i] = el; + } else { + int t = prev; + prev = tab[i]; + tab[i] = t; + } + } + (*nb)++; + return 1; +} + +int main(){ + int tab[10] = {0, 1, 3, 5}; + int taille = 10; + int nb = 4; + printf("%d\n\n", insertElt(-1, tab, taille, &nb)); + for (int i = 0; i < taille; i++){ + printf("%d. %d\n", i, tab[i]); + } +} |
