public static void Main() { int[] array = { 6, 1, 4, 5, 0, 7, 2, 3, 9, 8 ,23,56,89,4,56}; Sort(array); for (int i = 0; i < array.Length;i++ ) { Console.WriteLine(array[i]); } Console.ReadKey(); } public static void Sort(int []array) { int len = array.Length; int temp = 0; for (int i = 1; i <len;i++ ) { for (int k = 0; k <i;k++ ) { if (array[i] < array[k]) { temp=array[i]; while(i>k) { array[i] = array[i-1]; i--; } array[k] = temp; } } } }
本人菜鸟,逻辑上有不对的请指正,大家一同进步.....
现在进行修改,上面写的太傻比了:
public static void Sort(int[] array) { for (int i = 1; i < array.Length;i++ ) { int num = array[i]; while(i>0&&array[i-1]>num) { array[i] = array[i - 1]; i--; } array[i] = num; } }