c#插入排序
插入排序两个区域 未排序区 用一个索引值做分水岭未排序区元素与排序区元素比较插入到合适位置直到未排序区清空前提规则排序开始 时首先认为第一个元素在排序区中其他所有元素在未排序区排序开始后每次将未排序区第一个元素取出用于和 排序区中的元素比较满足条件则 排序区中元素往后移一个位置所有的数字都在一个数组中两个区域是一个分水岭索引第一步取出未排序区的所有元素进行比较i1的原因 默认第一个元素就在排序区for(int i 1; i array.Length; i)第二步每一轮取出排序区的最后一个元素索引int sortIndex i - 1;int noSortNum array[i];第三步 在未排序区进行比较移动位置确定插入索引循环停止的条件1发现排序区中所有元素都已经比较完2发现排序区中的元素不满足比较条件了while (sortIndex 0 array[sortIndex] noSortNum) { //只要进了这个while循环 证明满足条件 //排序区中的元素 就应该往后退一格 array[sortIndex 1] array[sortIndex]; //移动到排序区的前一个位置 准备继续比较 --sortIndex; } array[sortIndex 1] noSortNum;全程for(int i 1; i array.Length; i) { int sortIndex i - 1; int noSortNum array[i]; while (sortIndex 0 array[sortIndex] noSortNum) { array[sortIndex 1] array[sortIndex]; --sortIndex; } array[sortIndex 1] noSortNum; for(int i 0; i array.Length; i) { Console.WriteLine(array[i]); }