教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 文库大全 > 高中教育 >

数据结构(C语言版)习题及答案第九章(2)

来源:网络收集 时间:2026-09-04
导读: Rtype temp=r.e[i]; r.e[i]=r.e[i+1]; r.e[i+1]=temp; flag=1; 数据结构(C语言版)习题及答案 } i+=2; } i=2; while(in) { if (r.e[i].key r.e[i+1].key) { Rtype temp=r.e[i]; r.e[i]=r.e[i+1]; r.e[i+1]=temp; fla

Rtype temp=r.e[i];

r.e[i]=r.e[i+1];

r.e[i+1]=temp;

flag=1;

数据结构(C语言版)习题及答案

}

i+=2;

}

i=2;

while(i<n)

{

if (r.e[i].key> r.e[i+1].key)

{

Rtype temp=r.e[i];

r.e[i]=r.e[i+1];

r.e[i+1]=temp;

flag=1;

}

i+=2;

}

}

}

4 、设计一个双向冒泡排序算法,即在排序过程中交替改变扫描方向。(作为上机实践题目)

算法为:

struct Rtype

{

KeyType key; //关键字域

};

# define maxlen maxsize // maxsize为分配的存储单元个数

struct ListSq

{

Rtype e[ maxlen ] ; // 0号单元空闲,元素从1号单元开始存放

int len ;

}

void dbubblesort(ListSq r, int n)

{

int i,j,flag;

Rtype temp;

flag=1;

i=0;

while(flag)

{

flag=0;

for(j=n-i;j<i;j--)

{

if (r.e[j].key<r.e[j-1].key)

{

flag=a; temp=r.e[j];

r.e[j]= r.e[j-1];

数据结构(C语言版)习题及答案

r.e[j-1]=temp;

}

}

for(j=i;j<n-1;j++)

{

if (r.e[j].key>r.e[j+1].key)

{

flag=a; temp=r.e[j];

r.e[j]= r.e[j+1];

r.e[j+1]=temp;

}

}

i++;

}

}

5、写出快速排序的非递归算法。

Struct Elemtype

{

int low;

int high;

};

struct Stack

{

Elemtype stack [StackMaxSize] ; //StackMaxSize 确定顺序栈的最大深度 int top ; // 栈顶指针

};

struct Rtype

{

KeyType key; //关键字域

};

# define maxlen maxsize // maxsize为分配的存储单元个数

struct ListSq

{

Rtype e[ maxlen ] ; // 0号单元空闲,元素从1号单元开始存放

int len ;

}

void quicksort(ListSq *r, int s,int t)

{

Stack x;

Elemtype y,z,q;

Rtype temp;

Initstack(x);

y.low=s;

y.high=t;

push(x,y);

while(!Empty(x))

{

pop(s,y);

数据结构(C语言版)习题及答案

i=y.low; j=y.high;

temp=r.e[i];

do

{

while (j>i && r.e[j].key>=temp.key) j--;

if (i<j) //条件成立时交换r[i]和r[j]的值

{

r.e[i]=r.e[j];

i++;

}

while (i<j && r.e[i].key<=temp.key) i++;

if (i<j)

{

r[j]=r[i];

j--;

}

} while(i==j);//条件成立时继续进行一次划分中的比较和交换 r[i]=temp;

if ((i+1)<y.high)

{

z.low=i+1;

z.high=y.high;

push(x,z);

}

if (y.low<(i-1))

{

q.low=y.low;

q.high=i-1;

push(x,q);

}

}

}

数据结构(C语言版)习题及答案第九章(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wenku/131207.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)