- using System;
- namespace InsertionSort
- {
- /// <summary>
- /// 插入排序
- /// </summary>
- public class InsertionSort
- {
- public void Sort(int[] list)
- {
- for (int i = 1; i < list.Length; i++)
- {
- int temp = list[i];
- int j = i;
- while ((j > 0) && (list[j - 1] > temp))
- {
- list[j] = list[j - 1];
- j--;
- }
- list[j] = temp;
- }
- }
- static void Main(string[] args)
- {
- int[] test = new int[] { 1, 6, 3, 8, 11, 43, 0, 3, 57 };
- InsertionSort its = new InsertionSort();
- its.Sort(test);
- for (int i = 0; i < test.Length - 1; i++)
- {
- Console.WriteLine("{0}", test[i]);
- }
- Console.ReadKey();
- }
- }
- }
C#插入排序
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.