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


i have created program compares different search methods search random int value 0-999 sorted array 0-999. have created binary search works , after doing decided try create search which, instead of splitting values half, splits them 1/3 , 2/3 depending. if have {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15} , looking 10 go above {6,7,8,9,10,11,12,13,14,15} {10,11,12,13,14,15} {10,11} simple {10} , return index of value.

i 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 binary search is: " + loopaverage3 + "\n"); 

the code getting stuck running through first if statement , not positive of why.

any ideas issue or if logic close correct?

using same algorithm on smaller data set, can see issue. use array 3 members: 0 1 2. try find 2. third stuck on 1, , never high enough find 2.

this infinitely loop never getting third 2. may hitting similar window somewhere else in code. because enters first if, first = third + 1 yields first = 2. third=(first+last)/3=4/3=1.


Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -