(Java) A search similar to Binary but using /3 instead of /2

佐手、 提交于 2019-12-25 08:52:04

问题


I have created a program which compares different search methods which search for a random int value 0-999 from a sorted array which is 0-999. I have created a binary search which works perfectly and after doing this I decided to try to create a search which, instead of splitting the values into half, splits them into 1/3 and 2/3 depending. So basically if I have {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} and I was looking for 10 I would go from above to {6,7,8,9,10,11,12,13,14,15} to {10,11,12,13,14,15} to {10,11} then simple {10} and return the index of this value.

I currently have:

int loopTotal3 = 0;
    for(int y = 0; y < 1000; y++){
        System.out.println("Reference1");
        int first = 0;
        int last = array0Through999.length - 1;
        int third = (array0Through999[0] + array0Through999[999]) / 3;

        int findThis3 = rand.nextInt(1000);
        int loop3 = 0;
        while(first <= last){
            System.out.println("Reference2");
            loop3++;
             if (array0Through999[third] < findThis3){
                 System.out.println("Reference3");
                 first = third + 1;
             }
             else if(array0Through999[third] ==  findThis3){
                 System.out.println("Reference4");
                 break;
             }
             else{
                 System.out.println("Reference5");
                 last = third-1;
             }
             third = (first + last) / 3;
        }
        loopTotal3 = loopTotal3 + loop3;
    }
    int loopAverage3 = loopTotal3 / 1000;
    System.out.println("The average number of times for a Binary Search is: " + loopAverage3 + "\n");

The code is currently getting stuck running through the first if statement and I am not positive of why.

Any ideas about my issue or if this logic is close to correct?


回答1:


Using the same algorithm on a smaller data set, I can see an issue. Use an array with only 3 members: 0 1 2. Try to find 2. Third will get stuck on 1, and never get up high enough to find 2.

This will infinitely loop never getting third up to 2. You may be hitting a similar window somewhere else in the code. Because it enters the first if, it does first = third + 1 which yields first = 2. third=(first+last)/3=4/3=1.




回答2:


import java.util.Random;

public class weqfgtqertg {
    public static void main(String args[]) {
        int array0Through999[] = {0,1,...,999};
        int loopTotal3 = 0;
        Random rand = new Random();
        for(int y = 0; y < 1000; y++){
            //System.out.println("Reference1");
            System.out.println(y);
            int first = 0;
            int last = array0Through999.length - 1;
            int third = (first + last) / 3;

            int findThis3 = rand.nextInt(1000);
            int loop3 = 0;
            while(first <= last) {
                //System.out.println("Reference1");
                loop3++;
                 if (array0Through999[third] < findThis3){
                     //System.out.println("Reference3");
                     first = third+1;
                 }
                 else if(array0Through999[third] ==  findThis3){
                     //System.out.println("Reference4");
                     break;
                 }
                 else{
                     //System.out.println("Reference5");
                     last = third-1;
                 }
                 int calc = last - first;
                 third = first + (calc/3);
            }//end while
            loopTotal3 = loopTotal3 + loop3;
        }//end for
        int loopAverage3 = loopTotal3 / 1000;
        System.out.println("The average number of times for a Tertiary Search is: " + loopAverage3);
    }
}

It has been a while since I posted this question but I finally got around to solving my issue. Here is the correct code for anyone who may stumble upon this.

edit: The array includes the "..." to make this not obnoxious to read or put out onto the screen. I had all 0-999 within my array hard coded.



来源:https://stackoverflow.com/questions/40662277/java-a-search-similar-to-binary-but-using-3-instead-of-2

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