1029: 折半查找(2)*
Description
/*已知有序顺序表类LinearSearch类,计算折半查找等概论成功查找条件下的ASL值。
部分代码如下,勿改动,请补充Bin_Search和ASL_Bin_Search。*/
#include <iostream>
using namespace std;
const int MaxSize=100; //顺序表的最大长度
//有序表类
class LinearSearch{
public:
LinearSearch(){n=0;}
~LinearSearch(){}
void Insert(int x);//有序表的插入,使序列仍有序
void DispList(); //输出表
int Bin_Search(int key); //返回值为查找key值所需的比较次数
double ASL_Bin_Search(); //计算ASL值
private:
int r[MaxSize+1]; //存储元素(r[1]~r[n]存储元素)
int n; //顺序表实际长度
};
//在有序表中插入元素x,使序列仍有序
void LinearSearch::Insert(int x){
int i;
if (n>=MaxSize) //表满不能插入
throw "Overflow";
r[0]=x;
for(i=n;r[i]>x;i--)
r[i+1]=r[i];//将i位置元素后移
r[i+1]=x; //在位置i+1插入元素x
n++; //线性表长度增1
}
/*//计算ASL
double LinearSearch::ASL_Bin_Search()
{
int i,ASL=0;
for(i=1;i<=n;i++)
ASL+=Bin_Search(r[i]); //累加各个元素所需的比较次数
return 1.0*ASL/n;
}*/
void LinearSearch::DispList( ){ //输出线性表
int i;
cout<<"Data:";
for (i=1;i<=n;i++)
cout<<r[i]<<" ";
cout<<endl;
}
int main(){
LinearSearch A; //空表A
int x;
//利用插入函数创建有序表,以0结束
while(1){
cin>>x;
if(!x)break;
try{
A.Insert(x);
}
catch(char *wrong){
cout<<wrong<<endl;
}
}
A.DispList();
double ASL;
ASL=A.ASL_Bin_Search();
cout<<"ASL:"<<ASL<<endl;
return 0;
}
Input
Output
Sample Input
43 53 1 25 2 426 324 345 423 34 0
Sample Output
Data:1 2 25 34 43 53 324 345 423 426
ASL:2.9
//
// Created by Legends丶Hu on 2020/2/6.
//
#include <iostream>
using namespace std;
const int MaxSize = 100; //顺序表的最大长度
//有序表类
class SortList {
public:
SortList() { n = 0; }
~SortList() {}
void Insert(int x);//有序表的插入,使序列仍有序
void DispList(); //输出表
int Bin_Search(int key); //返回值为查找key值所需的比较次数
double ASL_Bin_Search();
private:
int r[MaxSize + 1]; //存储元素(r[1]~r[n]存储元素)
int n; //顺序表实际长度
};
int SortList::Bin_Search(int key) {
int left = 1, right = n;
int cnt = 0;
while (left <= right) {
int mid = (left + right) >> 1;
cnt++;
if(r[mid] < key)
left = mid + 1;
else if(r[mid] > key)
right = mid - 1;
else
return cnt;
}
return cnt;
}
//在有序表中插入元素x,使序列仍有序
void SortList::Insert(int x) {
int i;
if (n >= MaxSize) //表满不能插入
throw "Overflow";
r[0] = x;
for (i = n; r[i] > x; i--)
r[i + 1] = r[i];//将i位置元素后移
r[i + 1] = x; //在位置i+1插入元素x
n++; //线性表长度增1
}
//计算ASL
double SortList::ASL_Bin_Search() {
int i, ASL = 0;
for (i = 1; i <= n; i++)
ASL += Bin_Search(r[i]); //累加各个元素所需的比较次数
return 1.0 * ASL / n;
}
void SortList::DispList() { //输出线性表
int i;
cout << "Data:";
for (i = 1; i <= n; i++)
cout << r[i] << " ";
cout << endl;
}
//43 53 1 25 2 426 324 345 423 34 0
int main() {
SortList A; //空表A
int x;
//利用插入函数创建有序表,以0结束
while (1) {
cin >> x;
if (!x)break;
try {
A.Insert(x);
}
catch (char *wrong) {
cout << wrong << endl;
}
}
A.DispList();
double ASL;
ASL = A.ASL_Bin_Search();
cout << "ASL:" << ASL << endl;
return 0;
}