各越排序结果 2125 49 25 16 08 0 1 234 5 i- 1 21 25 49 25* 16 25 08 0 12345 temp 49 i=2 21 25 25* 49 16 08 0 12345 temp 11
11 各 趟 排 序 结 果 21 25 49 25* 16 08 0 1 2 3 4 5 0 1 2 3 4 5 temp 21 25 49 25* 16 08 i = 1 25 0 1 2 3 4 5 temp 21 25 49 25* 16 08 49 i = 2
i=3 2125 49 25* 16 25* 08 temp i=4 212525* 49 08 16 0 2345 temp 49 i=5 16 21 25 25* 08 0 1234 5 temp 12
12 0 1 2 3 4 5 i = 4 i = 5 i = 3 0 1 2 3 4 5 temp 21 25 49 25* 16 08 16 21 25 49 25* 16 08 25* 0 1 2 3 4 5 temp 21 25 49 16 25* 08 08 0 1 2 3 4 5 temp
16212525* 49 完成 08 0 1 2 3 4 5 三三三三三三三三三三三三 日三日目日三日三日目目目日目目目日目目三日目目三日目目三三三目三三三目三三三目三三 i=4时的排序过程 i=4 6 i=3 49 21 25 25* 08 16 0 23 45 temp i=4 j=2 212525* 6 949 08 16 1234 5 temp 13
13 21 25 49 16 25* 08 0 1 2 3 4 5 i = 4 时的排序过程 完成 16 16 0 1 2 3 4 5 temp 21 25 49 25* 08 16 49 16 0 1 2 3 4 5 temp 21 25 25* 49 08 16 i = 4 j = 3 i = 4 j = 2
i=4 i=1 21 25 16 25* 49 08 16 0 234 5 i=4 j=0 6 21 2525* 49 08 16 0 2 345 temp i=4 j=-1 16 21 25 25* 49 08 16 0 1234 5 temp 14
14 25 16 0 1 2 3 4 5 temp 21 49 25* 08 25 16 25* 16 21 25 49 25* 08 0 1 2 3 4 5 16 i = 4 j = 1 i = 4 j = 0 i = 4 j = -1 0 1 2 3 4 5 temp 21 25 25* 49 08 16 21 16
直接插入排序的算法 #include "dataList.h" template <class T> void InsertSort (dataList<T>&L,int left,int right) /依次将元素L.Vector[il按其排序码插入到有序表 /L.Vector[left],..,L.Vector[i-1l中,使得 /L.Vector[left到L.Vector[i有序。 Element<T>temp;int i,j; for (i=left+1;i<=right;i++) if(L[的<L[i-1]){ temp =L[i];j=i-1; 15
直接插入排序的算法 #include "dataList.h" template <class T> void InsertSort (dataList<T>& L, int left, int right) { //依次将元素L.Vector[i]按其排序码插入到有序表 //L.Vector[left],…,L.Vector[i-1]中,使得 //L.Vector[left]到L.Vector[i]有序。 Element<T> temp; int i, j; for (i = left+1; i <= right; i++) if (L[i] < L[i-1]){ temp = L[i]; j = i-1; 15