标签:
插入排序:逐个处理待排序的记录,每条记录与前面已排序的子列进行比较,将它插入到子序列的正确位置。
1 void InserionSort(int unsort[],const int count) 2 { 3 int i, j; 4 for (i = 1; i < count;i++) 5 { 6 int temp = unsort[i]; //选取做比较的位置 7 j = i - 1; //从前一个元素开始比较 8 while (j>0&&temp<unsort[j]) //如果被比较的元素比参照元素大,则将其后移 9 { 10 unsort[j+1] = unsort[j]; 11 j--; 12 } 13 if (j != i - 1) //满足条件,证明存在比参照量大的值 14 unsort[j+1]=temp; 15 } 16 }
测试
1 int main() 2 { 3 int unsort[] = {2,5,7,4,6,9}; 4 InserionSort(unsort, 6); 5 for (int i = 0; i < 6;i++) 6 { 7 printf("%d\r",unsort[i]); 8 } 9 return 0; 10 }
标签:
原文地址:http://www.cnblogs.com/jingliming/p/4330909.html