1. 程式人生 > >c# 插入排序

c# 插入排序

 class Program32
    {
        public static void Sort(int[] list)
        {
            for (int i = 1; i < list.Length; i++)
            {
                int t = list[i];
                int j = i;
                int tempInt = 0;
                while ((j>0)&&(list[j-1]>t)) //如果前一個元素大於當前元素,則交換
{ //大數依次往後排 list[j] = list[j-1]; --j; tempInt++; Console.WriteLine("while迴圈次數:" + tempInt); } list[j] = t; Console.WriteLine("" + i + "次排序後的結果:
"); foreach (var item in list) { Console.Write(item + " "); } Console.WriteLine(); } } static void Main(string[] args) { int[] iArrary = new int[] { 100, 98, 97, 96, 95, 94
}; Sort(iArrary); Console.ReadLine(); } }