插入排序

声明:引用网上经典的文章,特此声明

  就跟插牌一样,手里有牌之后,后面来的牌要插入到合适的位置去...

  C#代码如下,测试有效。s

代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sort
{
class Program
{
static void Main(string[] args)
{
int[] arr = new int[] { 49, 38, 65, 97, 76, 13, 27, 49 };
Console.Write(
"原数组数据顺序:");
foreach (int i in arr)
{
Console.Write(i
+ ".");
}
new iSort().InsertionSort(arr);
Console.Write(
"\n插入排序后数组数据顺序:");
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i]
+ ".");
Console.ReadLine();



}
}
}
class iSort
{
public int[] InsertionSort(int[] s)
{
for (int i = 0; i < s.Length - 1; i++) // s[0..i] 是每次迭代前的 L
{
int b = s[i + 1]; // s[0..i+1] 是迭代后的 L
int j = i;
while (j >= 0 && s[j] > b)
{
s[j
+ 1] = s[j];
j
--;
}
s[j
+ 1] = b;
}
return s;
}

}

 

posted @ 2010-10-25 12:15  arrow'  阅读(146)  评论(0编辑  收藏  举报