直接插入排序

来源:互联网 发布:三菱plc编程手册哪里有 编辑:程序博客网 时间:2024/05/21 07:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace 直接插入排序
{
    class Program
    {
        static void Main(string[] args)
        {
            int []s=  { 10,30,65964,12,64,1,3,9,75,94,110};
            int temp;
            for(int i=1;i<s.Length;i++)
            {
                for(int j=i;j>0;j--)//这里注意就J>0;之前我写成了>=0;那么会导致在循环遍历的时候可能
                {
                    if(s[j]>s[j-1])
                    {
                        temp = s[j];
                        s[j] = s[j-1 ];
                        s[j - 1] = temp;
                    }
                    else
                    {
                        break;
                    }
                }
            }
            for (int i = 0; i < s.Length; i++)
            {
                Console.Write(s[i]+"  ");
            }
            Console.ReadKey();
        }
    }
}
0 0
原创粉丝点击