以Int类型数组为举例
1 namespace 直接插入排序 2 { 3 class Program 4 { 5 6 private static void Insert(int[] arrayList) 7 { 8 bool isInsert = false; 9 for (int i = 1; i < arrayList.Length; i++) 10 { 11 int value = arrayList[i]; 12 for (int j = i-1; j >= 0; j--) 13 { 14 if (arrayList[j] > value) 15 { 16 arrayList[j + 1] = arrayList[i]; 17 } 18 else 19 { 20 //发现一个比i小的值, 21 arrayList[j+1] = value; 22 isInsert = true; 23 break; 24 25 } 26 if (isInsert == false) 27 { 28 arrayList[0] = value; 29 } 30 31 } 32 } 33 34 } 35 36 static void Main(string[] args) 37 { 38 int[] data = new[] {5, 10, 32, 40, 50, 55}; 39 Insert(data); 40 foreach (var temp in data) 41 { 42 Console.WriteLine(temp+" "); 43 } 44 Console.ReadKey(); 45 } 46 } 47 }
原文:http://www.cnblogs.com/pnzpb/p/5133136.html