Quick actions

cmd+k|ctrl+k

Navigation

Languages

Tri_Insertion

Snippet info

Language

Csharp

Visibility

public

Author

lea.romano

Created

2019-01-28T17:20:46Z

Updated

2019-01-28T17:32:36Z

using System;

class MainClass {
    
    public static void Afficher_tab(int[] tab)
        {
            for (int i = 0; i < tab.Length; i++)
            {
                Console.Write(tab[i] + " ");
            }
        }
        
    public static void Inserer(int element_a_inserer, int[] tab, int taille_gauche)
        {
            /*  On part de la fin de la main gauche, donc de taille gauche, et on descend (i--) 
             * tant que les cartes sont plus grandes que la carte à insérer. 
             * Le test j > 0 vérifie qu’on ne sort pas du tableau, 
             * ce qui pourrait arriver si toutes les cartes sont plus grandes que l’élément à insérer.  */
            int i;
            for (i = taille_gauche; i>0 && tab[i-1]> element_a_inserer;i--)
            {
                tab[i] = tab[i - 1];


            }
            tab[i] = element_a_inserer;
            /*La boucle s’arrête quand la carte tab[j-1] devient plus petite que l’élément à insérer. 
             * On insère alors cet élément juste après la case j-1, donc en j.  */

        }
        
        public static void Tri_Insertion(int[] tab)
        {
            //A CODER!!
        }
        
    public static void Main() {
        
        int[] tab = { 7, 13, 3, 18, 2 };
            Afficher_tab(tab);
            Tri_Insertion(tab);
            Console.WriteLine("\nTab trié");
            Afficher_tab(tab);
        
    }
}
INFO