插入排序简单实现

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

namespace InsertionSort
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
int[] arr = new int[] { 153629 };
            
int key;
            
int j;
            
int tmp;

            
for (int i = 1; i < arr.Length; ++i )
            {
                key 
= arr[i];

                
// 将数值插入到合适位置
                j = i - 1;
                
while ( (j > 0&& (arr[j] > key) ) // 升序
                {
                    
// 移动数据
                    arr[j + 1= arr[j];
                    
--j;
                }

                
// 找到了新加入的元素
                arr[j + 1= key;

            }

            
foreach (int item in arr)
            {
                Console.WriteLine(item);            
            }

            Console.ReadKey();
        }
    }
}
posted @ 2011-03-11 21:38  qiang.xu  阅读(226)  评论(0编辑  收藏  举报