首页 > 编程语言 > 详细

算法排序之插入排序

时间:2015-12-04 09:12:56      阅读:250      评论:0      收藏:0      [点我收藏+]

首先看什么是插入排序,咋们不说什么理论看下面动画图

技术分享

上代码:

using System;
using System.Collections.Generic;
using System.Text;

namespace suanfatest
{
    class Program
    {
        static void Main(string[] args)
        {

            int temp = 0;
            int[] arr = { 6,5,3,1,8,7,2,4};


            Console.WriteLine("排序前的數組是:");
            
            foreach(int item in arr)
            {
                Console.Write(item + ", ");
            }


            for (int i = 1; i < arr.Length; i++)
            {
                for (int j = i; j > 0; j--)
                {
                    if (arr[j] < arr[j - 1])
                    {
                        temp = arr[j-1];
                        arr[j-1] = arr[j];
                        arr[j] = temp;
                    }
                }
            }

            Console.WriteLine("");

            Console.WriteLine("排序後的數組是:");

            foreach (int item in arr)
            {
                Console.Write(item + ", ");
            }

            Console.WriteLine();
            Console.ReadKey();
        }
    }
}

代码分析:

技术分享

下一个学习 选择排序......

算法排序之插入排序

原文:http://www.cnblogs.com/yzenet/p/5018275.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!