折半查找1
题目描述
已知有序顺序表类LinearSearch类,实现折半查找。部分代码如下,勿改动,
请补充Bin_Search和DispList函数。
输入
43 53 1 25 2 426 324 345 423 34 0 25
输出
Data:1 2 25 34 43 53 324 345 423 426
Find 25 success,position:3
题目描述
#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所在数组下标(位置),否则查找失败,返回0
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
}
//请在下面补充实现相关算法的C++函数实现
void LinearSearch::DispList()
{
cout << "Data:";
for (int i = 1; i <= n; i++)
{
cout << r[i] << " ";
}
cout << endl;
}
int LinearSearch::Bin_Search(int key)
{
int front = 1, back = n,mid;
while (front <= back)
{
mid = front + (back - front) / 2;
if (key < r[mid])
{
back = mid - 1;
}
else if (key > r[mid])
{
front = mid + 1;
}
else
{
return mid;
}
}
return 0;
}
int main() {
LinearSearch A; //空表A
int x, key;
//利用插入函数创建有序表,以0结束
while (1) {
cin >> x;
if (!x)break;
try {
A.Insert(x);
}
catch (const char* wrong) {
cout << wrong << endl;
}
}
A.DispList();
int pos;
cin >> key;
pos = A.Bin_Search(key);
if (!pos)//查找失败
cout << "Find " << key << " failure\n";
else cout << "Find " << key << " success,position:" << pos << endl;
return 0;
}
折半查找2
题目描述
已知有序顺序表类LinearSearch类,计算折半查找等概论成功查找条件下的ASL值。
部分代码如下,勿改动,请补充Bin_Search和ASL_Bin_Search。
输入
43 53 1 25 2 426 324 345 423 34 0
输出
Data:1 2 25 34 43 53 324 345 423 426
ASL:2.9
代码示例
#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
}
void LinearSearch::DispList()
{
cout << "Data:";
for (int i = 1; i <= n; i++)
{
cout << r[i] << " ";
}
cout << endl;
}
/*//计算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;
}*/
int LinearSearch::Bin_Search(int key) {
int front = 1, rear = n, mid, count = 0;
while (front <= rear)
{
count++;
mid = front + (rear - front) / 2;
if (key < r[mid])
{
rear = mid - 1;
}
else if (key > r[mid])
{
front = mid + 1;
}
else return count;
}
return count;
}
double LinearSearch::ASL_Bin_Search()
{
double s = 0;
for (int i = 1; i <= n; i++)
{
s += Bin_Search(r[i]);
}
return s / n;
}
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;
}
折半查找(递归实现)
题目描述
已知折半查找的部分代码如下,勿改动。请补充实现折半查找算法Bin_Search。
要求:实现方式为递归方法。
输入
43 53 1 25 2 426 324 345 423 34 0 43
输出
Data:1 2 25 34 43 53 324 345 423 426
Find 43 success,position:5
代码示例
#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); //调用下面的递归算法
int Bin_Search(int low, int high, int key); //递归算法,成功返回位置,否则返回0
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
}
void LinearSearch::DispList() //输出表
{
int i;
cout << "Data:";
for (i = 1; i <= n; i++)
{
cout << r[i] << " ";
}
cout << endl;
}
//在下面补充实现折半查找算法(两个函数Bin_Search,1个形参和3个形参的各一个)
int LinearSearch::Bin_Search(int key)
{
return LinearSearch::Bin_Search(1, n, key);
}
int LinearSearch::Bin_Search(int low, int high, int key)
{
int mid;
while (low <= high)
{
mid = low + (high - low) / 2;
if (key < r[mid])
{
return Bin_Search(low, mid - 1, key);
}
else if (key > r[mid])
{
return Bin_Search(mid + 1, high, key);
}
else
{
return mid;
}
}
return 0;
}
int main() {
LinearSearch A; //空表A
int x, key;
//利用插入函数创建有序表,以0结束
while (1) {
cin >> x;
if (!x)break;
try {
A.Insert(x);
}
catch (char* wrong) {
cout << wrong << endl;
}
}
A.DispList();
int pos;
cin >> key;
pos = A.Bin_Search(key);
if (!pos)//查找失败
cout << "Find " << key << " failure\n";
else cout << "Find " << key << " success,position:" << pos << endl;
return 0;
}