TP6: More and more and more K&R coding style
[Algorithmic_C.git] / TP6 / tas / tas.c
CommitLineData
249ff697
JB
1/******************************************************************************/
2/* tas */
3/******************************************************************************/
4
5#include <stdio.h>
6#include <stdlib.h>
7
8#define MAX 11
9
862ce8c4 10typedef int TAS[MAX]; /* la case d'indice 0 n'est pas utilisee */
249ff697
JB
11
12/* affichage du tas */
13void affiche_tas(TAS t, int n, char *message)
14{
862ce8c4 15 int i;
249ff697 16
862ce8c4
JB
17 printf("%s \n", message);
18 for (i = 1; i <= n; i++)
19 printf("%5d ", t[i]);
20 printf("\n");
21}
249ff697
JB
22
23/* initialisation du tas la valeur val */
24void init_tas(TAS t, int n, int val)
25{
862ce8c4 26 int i;
249ff697 27
862ce8c4
JB
28 for (i = 0; i <= n; i++)
29 t[i] = val;
30}
249ff697
JB
31
32/* descente de l'element se trouvant au sommet du tas, a l'indice 1 et pas 0 */
33void descente(TAS t, int g, int d)
34{
862ce8c4
JB
35 int i, j, x; /* i indice du pere - j indice du fils */
36 x = t[g];
37 i = g;
38 j = 2 * i;
39
40 while (j <= d) {
41 if (j < d)
42 if (t[j] < t[j + 1])
43 j++; /* j indice du fils choisi */
44 if (x < t[j]) {
45 t[i] = t[j];
46 i = j;
47 j *= 2;
48 } else
49 break;
50 }
51 t[i] = x;
249ff697
JB
52}
53
249ff697 54/* montee de l'element d'indice m */
862ce8c4 55void montee(TAS t, int m, int n)
249ff697 56{
862ce8c4
JB
57 int i, j, x; /* i indice du pere - j indice du fils */
58 x = t[m];
59 j = m;
60 i = j / 2;
61
62 while (i >= 1) {
63 if (x > t[i]) {
64 t[j] = t[i];
65 j = i;
66 i = j / 2;
67 } else
68 break;
69 }
70 t[j] = x;
249ff697
JB
71}
72
249ff697
JB
73/* remplacement de l'element d'indice m par nouv_val et mise a jour du tas */
74void change(TAS t, int nouv_val, int m, int n)
75{
862ce8c4
JB
76 if (nouv_val < t[m]) {
77 t[m] = nouv_val;
78 descente(t, m, n);
79 } else {
80 t[m] = nouv_val;
81 montee(t, m, n);
82 }
249ff697
JB
83}
84
862ce8c4 85/****************************************************************************/
249ff697 86main()
862ce8c4
JB
87{
88 int n = MAX - 1;
89 int i, g, d, x;
90 TAS t;
91
92 srand(time(NULL));
93 for (i = 1; i <= n; i++)
94 t[i] = rand() % MAX;
95 affiche_tas(t, n, "Le tableau initial");
96
97 g = (n / 2) + 1;
98 d = n;
99 while (g > 1)
100 descente(t, --g, d);
101
102 affiche_tas(t, n, "Apres contruction du tas");
103 change(t, 45, 7, 10);
104 affiche_tas(t, n, "Le tas apres change(t,45,7,10)");
105 change(t, -5, 3, 10);
106 affiche_tas(t, n, "Le tas apres change(t,-5,3,10)");
107
108 while (d > 1) {
109 x = t[1]; /* le max */
110 t[1] = t[d]; /* le dernier devient le premier du tableau */
111 t[d] = x; /* le max est place a la fin du tableau */
112 descente(t, g, --d); /* on fait descendre le nouveau premier elt a sa bonne place */
113 }
114 affiche_tas(t, n, "Le tas apres le tri");
115
116 for (i = 1; i < n; i++)
117 if (t[i] > t[i + 1]) {
118 printf("Erreur tri\n");
119 break;
120 }
121}
249ff697
JB
122
123/***************************************************************************
124Le tableau initial
125 10 7 1 9 4 7 3 2 4 10
126Apres contruction du tas
127 10 10 7 9 7 1 3 2 4 4
128Le tas apres change(t,45,7,10)
129 45 10 10 9 7 1 7 2 4 4
130Le tas apres change(t,-5,3,10)
131 45 10 7 9 7 1 -5 2 4 4
132Le tas apres le tri
133 -5 1 2 4 4 7 7 9 10 45
134***************************************************************************/