严蔚敏版数据结构C语言版参考答案第十章文档格式.docx
- 文档编号:14316764
- 上传时间:2022-10-22
- 格式:DOCX
- 页数:13
- 大小:22.41KB
严蔚敏版数据结构C语言版参考答案第十章文档格式.docx
《严蔚敏版数据结构C语言版参考答案第十章文档格式.docx》由会员分享,可在线阅读,更多相关《严蔚敏版数据结构C语言版参考答案第十章文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
}
}//Insert_Sort1
10.24
voidBiInsert_Sort(SqList&
L)//二路插入排序的算法
intd[MAXSIZE];
//辅助存储
x=L.r.key;
d=x;
first=1;
final=1;
for(i=2;
i<
=L.length;
i++)
=x)//插入前部
for(j=final;
d[j]>
j--)
d[j+1]=d[j];
d[j+1]=L.r[i].key;
final++;
else//插入后部
for(j=first;
d[j]<
j++)
d[j-1]=d[j];
d[(j-2)%MAXSIZE+1]=L.r[i].key;
first=(first-2)%MAXSIZE+1;
//这种形式的表达式是为了兼顾first=1的情况
}//for
for(i=first,j=1;
d[i];
i=i%MAXSIZE+1,j++)//将序列复制回去
L.r[j].key=d[i];
}//BiInsert_Sort
10.25
voidSLInsert_Sort(SLList&
L)//静态链表的插入排序算法
L.r[0].key=0;
L.r[0].next=1;
L.r[1].next=0;
//建初始循环链表
i++)//逐个插入
p=0;
x=L.r[i].key;
while(L.r[L.r[p].next].key<
x&
&
L.r[p].next)
p=L.r[p].next;
q=L.r[p].next;
L.r[p].next=i;
L.r[i].next=q;
p=L.r[0].next;
for(i=1;
L.length;
i++)//重排记录的位置
while(p<
i)p=L.r[p].next;
if(p!
=i)
L.r[p]<
->
L.r[i];
L.r[i].next=p;
p=q;
}//SLInsert_Sort
10.26
voidBubble_Sort1(inta[],intn)//对包含n个元素的数组a进行改进的冒泡排序
change=n-1;
//change指示上一趟冒泡中最后发生交换的元素
while(change)
for(c=0,i=0;
change;
if(a[i]>
a[i+1])
a[i]<
a[i+1];
c=i+1;
//c指示这一趟冒泡中发生交换的元素
change=c;
}//while
}//Bubble_Sort1
10.27
voidBubble_Sort2(inta[],intn)//相邻两趟是反方向起泡的冒泡排序算法
low=0;
high=n-1;
//冒泡的上下界
change=1;
while(low<
high&
change)
change=0;
for(i=low;
high;
i++)//从上向下起泡
high--;
//修改上界
for(i=high;
i>
low;
i--)//从下向上起泡
if(a[i]<
a[i-1])
a[i-1];
low++;
//修改下界
}//Bubble_Sort2
10.28
voidBubble_Sort3(inta[],intn)//对上一题的算法进行化简,循环体中只包含一次冒泡
intb[3];
//b[0]为冒泡的下界,b[2]为上界,b[1]无用
d=1;
b[0]=0;
b[2]=n-1;
//d为冒泡方向的标识,1为向上,-1为向下
while(b[0]<
b[2]&
for(i=b[1-d];
i!
=b[1+d];
i+=d)//统一的冒泡算法
if((a[i]-a[i+d])*d>
0)//注意这个交换条件
a[i+d];
b[1+d]-=d;
//修改边界
d*=-1;
//换个方向
}//Bubble_Sort3
10.29
voidOE_Sort(inta[],intn)//奇偶交换排序的算法
while(change)
n-1;
i+=2)//对所有奇数进行一趟比较
for(i=0;
i+=2)//对所有偶数进行一趟比较
}//OE_Sort
分析:
本算法的结束条件是连续两趟比较无交换发生
10.30
typedefstruct{
intlow;
inthigh;
}boundary;
//子序列的上下界类型
voidQSort_NotRecurve(intSQList&
L)//快速排序的非递归算法
low=1;
high=L.length;
InitStack(S);
//S的元素为boundary类型
!
StackEmpty(S))//注意排序结束的条件
if(high-low>
2)//如果当前子序列长度大于3且尚未排好序
pivot=Partition(L,low,high);
//进行一趟划分
if(high-pivot>
pivot-low)
Push(S,{pivot+1,high});
//把长的子序列边界入栈
high=pivot-1;
//短的子序列留待下次排序
else
Push(S,{low,pivot-1});
low=pivot+1;
}//if
elseif(low<
high-low<
3)//如果当前子序列长度小于3且尚未排好序
Easy_Sort(L,low,high);
//直接进行比较排序
low=high;
//当前子序列标志为已排好序
else//如果当前子序列已排好序但栈中还有未排序的子序列
Pop(S,a);
//从栈中取出一个子序列
low=a.low;
high=a.high;
}//QSort_NotRecurve
intPartition(SQList&
L,intlow,inthigh)//一趟划分的算法,与书上相同
L.r[0]=L.r[low];
pivotkey=L.r[low].key;
high)
L.r[high].key>
=pivotkey)
L.r[low]=L.r[high];
L.r[low].key<
L.r[high]=L.r[low];
L.r[low]=L.r[0];
returnlow;
}//Partition
voidEasy_Sort(SQList&
L,intlow,inthigh)//对长度小于3的子序列进行比较排序
if(high-low==1)//子序列只含两个元素
if(L.r[low].key>
L.r[high].key)L.r[low]<
L.r[high];
else//子序列含有三个元素
L.r[low+1].key)L.r[low]<
L.r[low+1];
if(L.r[low+1].key>
L.r[high].key)L.r[low+1]<
}//Easy_Sort
10.31
voidDivide(inta[],intn)//把数组a中所有值为负的记录调到非负的记录之前
a[high]>
=0)high--;
//以0作为虚拟的枢轴记录
a[low]<
a[high];
0)low++;
a[high
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 严蔚敏版 数据结构 语言版 参考答案 第十
![提示](https://static.bdocx.com/images/bang_tan.gif)