aboutsummaryrefslogtreecommitdiff
path: root/semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c
diff options
context:
space:
mode:
Diffstat (limited to 'semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c')
-rw-r--r--semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c b/semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c
new file mode 100644
index 0000000..cfea419
--- /dev/null
+++ b/semestre 2/informatique/tme/semaine3/18_tri_trois_valeurs.c
@@ -0,0 +1,38 @@
+#include <assert.h>
+
+void echange(int *a, int *b){
+ int t = *a;
+ *a = *b;
+ *b = t;
+}
+
+void tri(int *a, int *b){
+ if (*a <= *b) return;
+ echange(a,b);
+}
+
+void tri_3(int *a, int *b, int *c){
+ tri(a, b);
+ tri(a, c);
+ tri(b, c);
+}
+
+int main(){
+ int a = 2, b = 5;
+ echange(&a, &b);
+ assert(a == 5);
+ assert(b == 2);
+ tri(&a, &b);
+ assert(a == 2);
+ assert(b == 5);
+ int c = 8;
+ a = 4; b = 2;
+ tri_3(&a, &b, &c);
+ assert(a == 2 && b == 4 && c == 8);
+ a = 2; b = 4; c = 8;
+ tri_3(&a, &b, &c);
+ assert(a == 2 && b == 4 && c == 8);
+ a = 8; b = 4; c = 2;
+ tri_3(&a, &b, &c);
+ assert(a == 2 && b == 4 && c == 8);
+}