i=3 2125 49 16 0/25 212525 9 [o8 2 5 temp i=5 1621 2525 49 23 5 emp
25* 0 1 2 3 4 5 0 1 2 3 4 5 temp 16 0 1 2 3 4 5 temp 08
完成 a0816211252549 2 3 订=4时的排序过程 = 3 a121125125496 月16 0 1 23 45 temp 22125125 6 49 16 012 emp
16 49 0 1 2 3 4 5 0 1 2 3 4 5 temp 0 1 2 3 4 5 temp 完成
=1团团团 49 16 i=4 j=021 2525 49 08 16 4 5 temp = 2525米49 16 012345 emp
25 25* 0 1 2 3 4 5 0 1 2 3 4 5 temp 0 1 2 3 4 5 temp 21
直接插入排序的算法 template <class Type> void dataList <Type> :: Insertsort(i ∥按排序码Key非递减顺序对表进行排序 Element<Type> temp; int i,j; for(1=l; 1< CurrentSize; 1++ temp= Vector[i; for(j=i;j>0;j-)/从后向前顺序比较 if( temp< Vector[j-1 Vector=Vector[j-1; else break lector[]=temp
template <class Type> void dataList <Type> :: InsertSort ( ) { //按排序码 Key 非递减顺序对表进行排序 Element<Type> temp; int i, j; for ( i = 1; i < CurrentSize; i++ ) { temp = Vector[i]; for ( j = i; j > 0; j-- ) //从后向前顺序比较 if ( temp < Vector[j-1] ) Vector[j] = Vector[j-1]; else break; Vector[j] = temp;
算法分析 设待排序对象个数为 currentsize=n,则该算 法的主程序执行n-1趟。 g排序码比较次数和对象移动次数与对象排序 码的初始排列有失。使已按排序码从小到 最好情况下,排序前对 大有序,每趟只需与前面有序对象序列的最 后一个对象比较1次,移动2次对象,总的排序 码比较次数为m1,对象移动次数为2(m-1)
} }