49 i=3 2125 608 25 012345 4同园回同 0123 5 temp 5同 21 252549 b 012345 tem
21 25 49 25* 16 08 0 1 2 3 4 5 0 1 2 3 4 5 temp 21 25 49 25* 16 i = 4 08 0 1 2 3 4 5 temp 21 25 49 16 25* 08 i = 5 i = 3 25* 16 08
完成08(16 25|2s449 012345 i=4时的排序过程 j=3 2525449 08 012345 ten j=2 212525 49 08 012345 temp
16 49 16 16 21 25 49 16 25* 08 0 1 2 3 4 5 0 1 2 3 4 5 temp 21 25 49 25* 08 i = 4 时的排序过程 0 1 2 3 4 5 temp 21 25 25* 49 完成 16 08 16 i = 4 j = 3 i = 4 j = 2
49 08 16 2 =三 j=0 2525449 08 012345 temp i=4 j=-1 21 2525449 08 0 2345 temp
25 25* 16 16 21 25 49 25* 08 0 1 2 3 4 5 0 1 2 3 4 5 temp 21 49 25* 08 0 1 2 3 4 5 temp 21 25 25* 49 16 08 16 16 25 21 i = 4 j = 1 i = 4 j = 0 i = 4 j = -1 16
直接插入排序的算法 template <class Type> void Insertion sort datalist<Type> list)( /按关键码Ke非递减顺序对表进行排序 for( int i=l; i< list. CurrentSize; i++) Insert( list, i ) template <class Type> viod Insert( datalist<Type>& list, int i)t Element<Type> temp= list. Vectorial; int=lg /从后向前顺序比较
直接插入排序的算法 template <class Type> void InsertionSort ( datalist<Type> & list ) { //按关键码 Key 非递减顺序对表进行排序 for ( int i = 1; i < list.CurrentSize; i++ ) Insert ( list, i ); } template <class Type> viod Insert ( datalist<Type> & list, int i ) { Element<Type> temp = list.Vector[i]; int j = i; //从后向前顺序比较
while (i>o & temp. getKey ( list Vectorl-1 getKey() f list. Vector]=list. Vector-1;j-3 list. Vector= temp; 算法分析 若设待排序的对象个数为 curremtsize=n,则该 算法的主程序执行n-1趟。 关键码比较次数和对象移动次数与对象关键码 的初始排列有关
算法分析 若设待排序的对象个数为curremtsize = n,则该 算法的主程序执行n-1趟。 关键码比较次数和对象移动次数与对象关键码 的初始排列有关。 while ( j > 0 && temp.getKey ( ) < list.Vector[j-1].getKey ( ) ) { list.Vector[j] = list.Vector[j-1]; j--; } list.Vector[j] = temp; }