2道公司面试题目....求解~

prettyladys 2003-12-25 10:49:36
堆排序算法,编写.被排序的是整数,由函数rand( )产生n个整数,也可以从文件读如n个整数,n的数值从键盘读入
显示输入数据:(1)n的数值(2)n个整数数值.注意:n的数值不小于100
显示输出结果:1排序结果,即排序之后的数据,将它们五个一行或者十个一行.2比较次数,即排序过程中的总比较次数.3换位次数,即排序过程中的总交换操作次数,以显示程序运行正确.

2 B树
关键字是整数.由函数rand( )产生n个整数,也可以从文件读如n个整数,n的数值从键盘读入.每个节点所能够容纳的关键字不少于8,树的关键字不能少于600.主函数必须能够执行至少四种功能.
建造B树
查询
插入
删除(给定一个关键字的值)
在插入(删除)之前,应当调用查询函数,如查询成功(失败)则不执行插入删除.
...全文
34 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
gphacker 2004-01-09
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>

int n;

struct HEAP
{
HEAP(int _n)
{
n=_n;
curr_point=1;
n_com=0;
n_exc=0;
Matrix=new int[n+1];
}
~HEAP(){delete [] Matrix;}
int n;
int curr_point;
int * Matrix;
int n_com;
int n_exc;
};

void InsertHeap(HEAP * h, int i)
{
int temp;
h->Matrix[h->curr_point]=i;
for(int j=h->curr_point;j>=2;)
{
if(h->Matrix[j]>h->Matrix[j/2])
{
h->n_com++;
h->n_exc++;
temp=h->Matrix[j];
h->Matrix[j]=h->Matrix[j/2];
h->Matrix[j/2]=temp;
j=j/2;
}
else
break;
}
h->curr_point++;
}

void InitHeap(HEAP * h,int m[])
{
for(int i=0;i<h->n;i++)
InsertHeap(h,m[i]);
}

int DeleteMax(HEAP * h)
{
int ret=h->Matrix[1];
int side;
h->curr_point--;
int temp=h->Matrix[h->curr_point];
for(int i=1;i<h->curr_point;i=side)
{
if(2*i+1<h->curr_point)
{
h->n_com++;
if(h->Matrix[2*i]>h->Matrix[2*i+1])
side=2*i;
else
side=2*i+1;
}
else if(2*i<h->curr_point)
side=2*i;
else
{
h->Matrix[i]=temp;
break;
}
if(h->Matrix[side]>temp)
{
h->n_exc++;
h->Matrix[i]=h->Matrix[side];
}
else
{
h->n_exc++;
h->Matrix[i]=temp;
break;
}
}
return ret;
}

void main()
{
printf("Input number:");
scanf("%d",&n);
int * m=new int [n];
for(int i=0;i<n;i++)
m[i]=rand();

HEAP h(n);
InitHeap(&h,m);

for(i=1;i<=n;i++)
{
printf("%d ",DeleteMax(&h));
if(i%5==0&&i>0)
printf("\n");
}
printf("\ncompare:\t%d\nexchang:\t%d\n",h.n_com,h.n_exc);
}
comeonstuding 2003-12-26
  • 打赏
  • 举报
回复
p
prettyladys 2003-12-26
  • 打赏
  • 举报
回复
UP
zhang_jiang 2003-12-25
  • 打赏
  • 举报
回复
#include <stdlib.h>
#include <stdio.h>

int n;

struct HEAP
{
HEAP(int _n)
{
n=_n;
curr_point=1;
n_com=0;
n_exc=0;
Matrix=new int[n+1];
}
~HEAP(){delete [] Matrix;}
int n;
int curr_point;
int * Matrix;
int n_com;
int n_exc;
};

void InsertHeap(HEAP * h, int i)
{
int temp;
h->Matrix[h->curr_point]=i;
for(int j=h->curr_point;j>=2;)
{
if(h->Matrix[j]>h->Matrix[j/2])
{
h->n_com++;
h->n_exc++;
temp=h->Matrix[j];
h->Matrix[j]=h->Matrix[j/2];
h->Matrix[j/2]=temp;
j=j/2;
}
else
break;
}
h->curr_point++;
}

void InitHeap(HEAP * h,int m[])
{
for(int i=0;i<h->n;i++)
InsertHeap(h,m[i]);
}

int DeleteMax(HEAP * h)
{
int ret=h->Matrix[1];
int side;
h->curr_point--;
int temp=h->Matrix[h->curr_point];
for(int i=1;i<h->curr_point;i=side)
{
if(2*i+1<h->curr_point)
{
h->n_com++;
if(h->Matrix[2*i]>h->Matrix[2*i+1])
side=2*i;
else
side=2*i+1;
}
else if(2*i<h->curr_point)
side=2*i;
else
{
h->Matrix[i]=temp;
break;
}
if(h->Matrix[side]>temp)
{
h->n_exc++;
h->Matrix[i]=h->Matrix[side];
}
else
{
h->n_exc++;
h->Matrix[i]=temp;
break;
}
}
return ret;
}

void main()
{
printf("Input number:");
scanf("%d",&n);
int * m=new int [n];
for(int i=0;i<n;i++)
m[i]=rand();

HEAP h(n);
InitHeap(&h,m);

for(i=1;i<=n;i++)
{
printf("%d ",DeleteMax(&h));
if(i%5==0&&i>0)
printf("\n");
}
printf("\ncompare:\t%d\nexchang:\t%d\n",h.n_com,h.n_exc);
}

33,029

社区成员

发帖
与我相关
我的任务
社区描述
数据结构与算法相关内容讨论专区
社区管理员
  • 数据结构与算法社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧