Binary Interpolation Searching - Cant make it work

ε祈祈猫儿з 提交于 2019-12-25 09:45:28

问题


I got an exercise in the university to make the Binary Interpolation Searching Program . But i cant make it work as it should. Sometimes is works and some other not...They gave us the basic program but it was not correct..

#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;
} 

I worked on it for hours but still crashing.... So this is what i did :

#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 my code(the second):

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

Output:---
Crashing..

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

Output:---
Never ends....

Also when i change the "for" loop in the beggining (to fill the array) in line 22: matrix[i] = i; and change it to matrix[i] = i*5 ; so i can test what happens when it cant find it...It never ends too....

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

Output:---
Never ends....

Thanks in advance!

来源:https://stackoverflow.com/questions/43644787/binary-interpolation-searching-cant-make-it-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!