[c] tri
Fermé
prob
-
26 janv. 2008 à 17:03
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 - 27 janv. 2008 à 21:39
lami20j Messages postés 21331 Date d'inscription jeudi 4 novembre 2004 Statut Modérateur, Contributeur sécurité Dernière intervention 30 octobre 2019 - 27 janv. 2008 à 21:39
2 réponses
lami20j
Messages postés
21331
Date d'inscription
jeudi 4 novembre 2004
Statut
Modérateur, Contributeur sécurité
Dernière intervention
30 octobre 2019
3 566
27 janv. 2008 à 21:39
27 janv. 2008 à 21:39
Salut,
mieux vaux de structurer ton programme :
fonction pour menu
fonction pour affichage
fonction pour tri_extraction
fonction pour tri_insertion
fonction pour tri à bulle
voici le code (à toi d'adapter )
mieux vaux de structurer ton programme :
fonction pour menu
fonction pour affichage
fonction pour tri_extraction
fonction pour tri_insertion
fonction pour tri à bulle
voici le code (à toi d'adapter )
#include<stdio.h>
#define TAILLE 5
#define VRAI 1
#define FAUX 0
void tri_extraction (int *v);
void tri_insertion (int *v);
void tri_bulle (int *v);
int menu ();
void affiche (int *v);
int main ()
{
int v[TAILLE];
int i, choix;
for (i = 0; i < TAILLE; ++i){
printf ("Entrez l'élément %d : ", i + 1);
scanf ("%d", &v[i]);
}
printf("Le tableau initial : ");
affiche (v);
do{
choix = menu ();
switch (choix){
case 1:
printf ("Tri par extraction\n");
tri_extraction (v);
affiche (v);
break;
case 2:
printf ("Tri insertion\n");
tri_insertion (v);
affiche (v);
break;
case 3:
printf ("Tri bulle\n");
tri_bulle (v);
affiche (v);
break;
case 4:
printf ("Au revoir!\n");
return 0;
default:
printf ("Option inconnue!\n");
break;
}
}while (choix > 0 && choix < 4);
return 0;
}
/* affiche le tableau */
void affiche (int *v)
{
int i;
for (i = 0; i < TAILLE; ++i)
printf ("%d ", v[i]);
printf ("\n");
}
/* menu */
int menu ()
{
int choix;
printf ("********** MENU **********\n");
printf ("1. tri extraction\n");
printf ("2. tri insertion\n");
printf ("3. tri bulle\n");
printf ("4. Quitter\n");
printf ("\nChoisissez une option : ");
scanf ("%d", &choix);
return choix;
}
/* tri par extraction */
void tri_extraction (int *v)
{
int i, min, j, x;
for (i = 0; i < TAILLE - 1; i++){
min = i;
for (j = i + 1; j < TAILLE; j++)
if (v[j] < v[min])
min = j;
if (min != i){
x = v[i];
v[i] = v[min];
v[min] = x;
}
}
}
/* tri par insertion */
void tri_insertion (int *v)
{
int i, j, p, x;
for (i = 1; i < TAILLE; i++){
x = v[i];
for (p = 0; v[p] < x; p++);
for (j = i - 1; j >= p; j--)
v[j + 1] = v[j];
v[p] = x;
}
}
/* tri à bulle */
void tri_bulle (int *v)
{
int i, j, tmp;
int etat = VRAI;
i = j = tmp = 0;
for (i = 0; (i < TAILLE) && etat; i++){
etat = FAUX;
for (j = 1; j < TAILLE - i; j++){
if (v[j] < v[j - 1]){
tmp = v[j - 1];
v[j - 1] = v[j];
v[j] = tmp;
etat = VRAI;
}
}
}
}
bmv5
Messages postés
61
Date d'inscription
samedi 26 janvier 2008
Statut
Membre
Dernière intervention
1 mai 2008
26 janv. 2008 à 17:09
26 janv. 2008 à 17:09
salut ,
J'ai fait un fichier trih.bat dont j'ai copié ton modèle sur bureau et voici le résultat :
J'espère que je vous aide .
A+
J'ai fait un fichier trih.bat dont j'ai copié ton modèle sur bureau et voici le résultat :
Microsoft Windows XP [version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Documents and Settings\HP\Bureau>TRIH.BAT La syntaxe de la commande est incorrecte. C:\Documents and Settings\HP\Bureau>#include <stdio.h> C:\Documents and Settings\HP\Bureau>EXIT
J'espère que je vous aide .
A+