c#: Trier des tableaux et des matrices
Exemple
Donc en sortie: <13,42,66,91,123,132>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TriBubble
{
class Program
{
static void Main(string[] args)
{
const int MaxTableau = 8;
int K,L,I,J;
int[] Tableau = { 15, 10, 23, 2, 8, 9, 14, 16 };
Console.Write("Avant:");
for(K = 0; K < MaxTableau; K++) Console.Write(Tableau[K] + ", ");
for(I = MaxTableau - 2;I >= 0; I--) {
for(J = 0; J <= I; J++) {
if(Tableau[J + 1] < Tableau[J]) {
int t = Tableau[J + 1];
Tableau[J + 1] = Tableau[J];
Tableau[J] = t;
}
}
}
Console.WriteLine();
Console.Write("Après:");
for(L = 0; L < MaxTableau; L++) {
Console.Write(", " + Tableau[L]);
}
Console.WriteLine();
}
}
}
Exercices d’applicationCorrection
Correction
Correction
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace exercice3
{
class Program
{
static int [] tab=new int[10];
static void Main(string[] args)
{
exercice_3();
TriTabCroissant();
TriTabDecroissant();
}
static void exercice_3()
{
for(int i=0;i<10;i++)
{
Console.WriteLine("Saisir lélémént {0}" , i+1);
//if(isNumeric(Console.ReadLine()))
//{
tab[i] =Convert.ToInt32( Console.ReadLine());
//}
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine("L'élémént {0} est : {1}" ,i, tab[i]);
}
}
static void TriTabCroissant()
{
Console.WriteLine("Triage du tableau dans l'ordre croissant");
int t;
//Triage du tableau
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10 - 1; j++)
{
if (tab[j] > tab[j + 1])
{
t = tab[j + 1];
tab[j + 1] = tab[j];
tab[j] = t;
}
}
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(tab[i]);
}
}
static void TriTabDecroissant()
{
Console.WriteLine("Triage du tableau dans l'ordre décroissant");
int t;
//Triage du tableau
for (int i = 0; i < 9; i++)
{
for (int j = 0; j < 10 - 1; j++)
{
if (tab[j] < tab[j + 1])
{
t = tab[j + 1];
tab[j + 1] = tab[j];
tab[j] = t;
}
}
}
for (int i = 0; i < 10; i++)
{
Console.WriteLine(tab[i]);
}
}
}
}
|
