C# Tri d’un tableau par selection

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
}
}
}



 

This entry was posted in C#Sharp, Codes and tagged , , , , , , , , , , , . Bookmark the permalink.

One Response to C# Tri d’un tableau par selection

  1. Pingback: CDI – Concepts et programmation orientée objet | Clogique

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

*

Vous pouvez utiliser ces balises et attributs HTML : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>