Nous désignerons par a1, a2, …, an les éléments d’un tableau à trier par ordre croissant.
On commence par chercher l’indice du plus petit des éléments, soit j cet indice. On permute alors les valeurs de a1 et aj .On cherche ensuite l’indice du plus petit des éléments a2, a3, …, an et on permute avec a2, etc.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercice_tableau_4_tri
{
class Program
{
static void Main(string[] args)
{
//declaration
int[] table = new int[] { 21, 1, 0, 5, 8, 2, 88, 17, 5, 17 };
//traitement
for (int k = 0; k < table.Length; k++)
{
//trouver le plus petit
int j = k;
for (int i = k; i < table.Length; i++)
{
if (table[i] < table[j])
{
j = i;
}
}
//permuter
int tmp = table[k];
table[k] = table[j];
table[j] = tmp;
}
for (int i = 0; i < table.Length; i++)
{
Console.WriteLine("rang : " +i+ "\tvaleur : " + table[i]);
}
Console.ReadKey();
//fin
}
}
}
Flux RSS Clogique.com
Pingback: CDI – Concepts et programmation orientée objet | Clogique