我在大学里练习进行二进制插值搜索 计划。但我不能让它按预期工作。有时是工作,有些不是......他们给了我们基本的程序,但它不正确..
#include <iostream>
#include <math.h>
using namespace std;
void bin(int mat_size, double *mat, int key){
int left = 0;
int right = mat_size;
int mid;
mid = (mat_size * (key - mat[left]) / (mat[right] - mat[left])) + 1;
while(key != mat[mid]){
int i = 0;
mat_size = right - left +1;
if(key >= mat[mid]){
while(key > mat[(int)(mid + i * sqrt(mat_size) - 1)]){
i++;
}
right = mid + i * sqrt(mat_size);
left = mid + (i - 1) * sqrt(mat_size);
}
else if( key < mat[mid]){
while(key < mat[(int)(mid - i * sqrt(mat_size) + 1)]){
i++;
}
right = mid - (i - 1) * sqrt(mat_size);
left = mid - i * sqrt(mat_size);
}
mid = left + ((right - left + 1) * ((key - mat[left]) / (mat[right] - mat[left])));
}
if(key == mat[mid]){
cout<<mat[mid];
}
else{
cout<<"Key not found";
}
}
int main()
{
cout<<"Hello enter the size of the array!"<<endl;
int matrix_num;
cin>>matrix_num;
cout<<"Enter the values of the array"<<endl;
cout<<"Caution elements must be sorted in order to use Binary Search"<<endl;
double *matrix;
matrix = new double[matrix_num];
for(int i=0; i<matrix_num; i++){
cin>>matrix[i];
}
cout<<"Enter a key you wish to search for"<<endl;
int key;
cin>>key;
bin(matrix_num, matrix, key);
delete [] matrix;
}
我已经工作了几个小时但仍然崩溃...... 所以这就是我所做的:
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <iostream>
#include <math.h>
//Binary Interpolation Searching !
using namespace std;
int main()
{
cout << "Hello enter the size of the array!" << endl;
int matrix_num;
cin >> matrix_num;
cout << "Enter the values of the array" << endl;
cout << "Caution elements must be sorted in order to use Binary Search" << endl;
int *matrix;
matrix = new int[matrix_num];
for (int i = 0; i<matrix_num; i++) {
matrix[i] = i ;
cout << matrix[i] << endl;
}
cout << "Enter a key you wish to search for" << endl;
int key;
cin >> key;
int left = 1; // aristera =0
int right = matrix_num - 1; // de3ia = teleuteo stoixeio
int mid; // kentro
int flag = 0;
if (key <= matrix[right]) {
mid = (matrix_num * (key - matrix[left]) / (matrix[right] - matrix[left])) + 1;
while ((key != matrix[mid]) && (flag == 0)) {
int i = 0;
matrix_num = right - left + 1;
if (key >= matrix[mid]) {
while (key > matrix[(int)(mid + i * sqrt(matrix_num) - 1)]) {
i++;
}
right = mid + i * sqrt(matrix_num);
left = mid + (i - 1) * sqrt(matrix_num);
}
else if (key < matrix[mid]) {
while (key < matrix[(int)(mid - i * sqrt(matrix_num) + 1)]) {
i++;
}
right = mid - (i - 1) * sqrt(matrix_num);
left = mid - i * sqrt(matrix_num);
}
if (matrix[left] == matrix[right]) {
flag++;
}
mid = left + ((right - left + 1) * ((key - matrix[left]) / (matrix[right] - matrix[left])));
}
}
if (key == matrix[mid]) {
cout << matrix[mid];
}
else {
cout << "Key not found"<<endl;
system("Pause");
}
delete[] matrix;
}
在我的代码中(第二个):
In the first question "Hello enter the size of the array!":
Input:500
In the second question "Enter a key you wish to search for":
Input:499
输出:---
崩溃..
In the first question "Hello enter the size of the array!":
Input:500
In the second question "Enter a key you wish to search for":
Input:498
输出:---
永无止境......
当我更改第22行中的beggining(填充数组)中的“for”循环时:matrix [i] = i;并将其更改为矩阵[i] = i * 5; 所以我可以测试当它找不到时会发生什么......它永远不会结束......
In the first question "Hello enter the size of the array!":
Input:500
In the second question "Enter a key you wish to search for":
Input:498
输出:---
永无止境......
提前致谢!