您的位置:首页 > 编程语言 > C#

C# 插入排序算法

2016-05-30 22:56 477 查看
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

int[] a = { 118, 101, 105, 127, 112 };

InsertSort(a);

Console.ReadLine();
}

static void InsertSort(int[] array)
{
int j, t;

for (int i = 1; i < array.Length; i++)
{
t = array[i];
j = i - 1;

while (j >= 0 && t < array[j])
{
array[j + 1] = array[j];
j--;
}

array[j + 1] = t;

Console.WriteLine("第" + i + "步排序结果");

for (int h = 0; h < array.Length; h++)
{
Console.Write(" " + array[h].ToString());
}
Console.WriteLine();
}

}

}

}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: