实验三 查找与排序

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

实验三查找与排序
一、实验目的和要求:
掌握运用数据结构两种基本运算查找和排序,并能通过其能解决应用问题。

实验要求:
1.认真阅读和掌握本实验的算法。

2.上机将本算法实现。

3.观察程序的运行结果,并结合程序进行分析。

4. 按实验内容的要求,编写程序;提交实验报告;
二、实验环境:
计算机
操作系统Wndows XP
编程工具TURBO C 2.0
三、实验内容(一):
1. 屏幕提示用户任意输入10个整数,组成一个待排序序列。

2. 使用任意两种排序算法对序列进行排序,输出每趟排序的结果。

3. 使用折半查找法查找序列中的值,查找内容由系统提示用户输入,输出查找位置。

三、实验内容(二):
为宿舍管理人员编写一个宿舍管理查询软件,程序采用交互工作方式,其流程如下:开始,建立数据文件,
数据文件按关键字(姓名、学号、房号)进行排序(冒泡、选择、快速等任选一种)查询菜单:(用二分查找实现以下操作)
1.按姓名查询
2.按学号查询
3.按房号查询
打印任一查询结果(可以连续操作)
四、源程序
(一)
#include <stdio.h>
void SelectSort(int R[],int n)
{ int i,j,k,t;
for(i=0;i<n-1;i++)
{ k=i;
for(j=i+1;j<n;j++)
if(R[j]<R[k])k=j;
if(k!=i)
{temp=R[i]; R[i]=R[k];R[k]=temp;} for(t=0;t<n;t++)
printf("%d ",R[t]);}}
search(int b[],int key,int n)
{ int high,mid,low,i=0;
low=1;
high=n;
while(low<=high)
{mid=(low+high)/2;
if(key<b[mid])high=mid-1;
else
if(key>b[mid])low=mid+1;
else
{i=mid;break;}
}
return(i);
}
void main()
{ int a[10];
int i,j;
printf("输入数据\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
SelectSort(a,10);
printf("the key number:\n"); scanf("%d",&j);
i=search(a,j,10);
printf("\n");
printf("the position is:\n");
printf("%d",i+1);
}
15
35
55
40
20
23
18
28
32
50
15 35 55 40 20 23 18 28 32 50 15 18 55 40 20 23 35 28 32 50 15 18 20 40 55 23 35 28 32 50 15 18 20 23 55 40 35 28 32 50 15 18 20 23 28 40 35 55 32 50 15 18 20 23 28 32 35 55 40 50 15 18 20 23 28 32 35 55 40 50 15 18 20 23 28 32 35 40 55 50 15 18 20 23 28 32 35 40 50 55 The key number:
28
The position is:
5
(二)
#include <stdio.h>
void insertsort(int a[],int n) { int i,j,temp,k;
for(j=0;j<n-1;j++)
{for(i=0;i<n-j-1;i++)
if(a[i]>a[i+1])
{ temp=a[i];
a[i]=a[i+1];
a[i+1]=temp;}
for(k=0;k<n;k++)
printf("%d ",a[k]);
printf("\n");}}
search(int b[],int key,int n) { int high,mid,low,i=0;
low=1;
high=n;
while(low<=high)
{mid=(low+high)/2;
if(key<b[mid])high=mid-1; else
if(key>b[mid])low=mid+1; else
{i=mid;break;}
}
return(i);
}
void main()
{ int a[10];
int i,j;
printf("输入数据\n");
for(i=0;i<10;i++)
scanf("%d",&a[i]);
printf("\n");
insertsort(a,10);
printf("the key number:\n"); scanf("%d",&j);
i=search(a,j,10);
printf("\n");
printf("the position is:\n"); printf("%d",i+1);}
运行结果:
42
25
43
37
5
10
67
49
26
89
25 42 37 5 10 43 49 26 67 89 25 37 5 10 42 43 26 49 67 89 25 5 10 37 42 26 43 49 67 89 5 10 25 37 26 42 43 49 67 89 5 10 25 26 37 42 43 49 67 89 5 10 25 26 37 42 43 49 67 89 5 10 25 26 37 42 43 49 67 89 5 10 25 26 37 42 43 49 67 89 5 10 25 26 37 42 43 49 67 89
The key number is:
42
The position is:
6。

相关文档
最新文档