Jump to content

Java binary search help

Go to solution Solved by auraofjason,

A double is a floating point number (like 1.1234) while an int is just a whole number (like 1).

 

You can keep mid and SIZE as doubles, but when you iterate with them you can cast them into ints like: (int)mid. So for example in this line instead of: if (a[mid]>x), you can do: if (a[(int)mid]>x).

Ok so I'm in a beginner java class and need help with the errors im getting in this program any help would be great.

 

CODE:

 public class BinSearch{public static final int NOT_FOUND = -1;public static double binarySearch(double[] a, double x){double low=0;double high= a.length-1;double mid;while (low<=high){mid = (low+high)/2;if (a[mid]>x)low = mid+1;if (a[mid]<x)high = mid-1;elsereturn mid;}return NOT_FOUND;} public static void main(String[] args){double SIZE =43.5;double[] a = new double[SIZE];for (double i=0; i<SIZE;i++)a[i] = new Double(i * 2);for (double i=0; i<Size*2; i++)System.out.println("Found"+i+ "at"+binarySearch(a, new Double(i)));}}
 

 

The errors consist with a few Loss of precision and one cannot find symbol

 

Once again thanks for the help.

Edited by alpenwasser
added code tags

Processor-Intel - Core i9-7900X 3.3GHz , Motherboard- Gigabyte - X299 AORUS Gaming 7, RAM- G.Skill - TridentZ RGB 32GB (4 x 8GB) DDR4-4000 Memory, GPU-Zotac - GeForce GTX 1080 Ti 11GB AMP Extreme,  Case- Phanteks - Enthoo Evolv ATX Glass, Storage- Samsung - 960 EVO 250GB M.2-2280, Samsung 850 Evo 250GB SSD, Samsung 850 EVO 1TB SSD, Toshiba 4TB (PH3400U) , PSU- SeaSonic 1200W Platinum, Cooling- NZXT - Kraken X62 Rev 2, Sound-Sennheiser - HD 598SE, SURE - SM7B, OS- Windows 10 Pro.

Link to comment
Share on other sites

Link to post
Share on other sites

Loss of precision: You can't iterate through a double[] with a double, it must be an int. You can cast a double into an int by doing: (int)double, or (int)mid for your mid double.

 

Cannot find symbol: You set the variable name to be SIZE, but you used Size in your for loop. SIZE and Size are different.

i9-11900K @ 5.1ghz;   RTX 3090 FE @ 2ghz;   ROG Maximus XII Hero;   4266mhz G.Skill 16GB DDR4;

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so i fixed the SIZE issue, thanks, but I'm still confused by the double.  

 

I turned the double mid to int mid and I am still getting errors of loss of precision. specifically at  mid = (low+high)/2, and int SIZE =43.5;.

Processor-Intel - Core i9-7900X 3.3GHz , Motherboard- Gigabyte - X299 AORUS Gaming 7, RAM- G.Skill - TridentZ RGB 32GB (4 x 8GB) DDR4-4000 Memory, GPU-Zotac - GeForce GTX 1080 Ti 11GB AMP Extreme,  Case- Phanteks - Enthoo Evolv ATX Glass, Storage- Samsung - 960 EVO 250GB M.2-2280, Samsung 850 Evo 250GB SSD, Samsung 850 EVO 1TB SSD, Toshiba 4TB (PH3400U) , PSU- SeaSonic 1200W Platinum, Cooling- NZXT - Kraken X62 Rev 2, Sound-Sennheiser - HD 598SE, SURE - SM7B, OS- Windows 10 Pro.

Link to comment
Share on other sites

Link to post
Share on other sites

Ok so i fixed the SIZE issue, thanks, but I'm still confused by the double.  

 

I turned the double mid to int mid and I am still getting errors of loss of precision. specifically at  mid = (low+high)/2, and int SIZE =43.5;.

int SIZE =43.5 makes no sense because an int can't have decimals. Just drop the .5

The loss of precision is because low and high are still doubles which makes (low+high)/2 a double and you lose precision by going to an int. Either change low and high to ints or cast the result of (low+high)/2 to an int.

1474412270.2748842

Link to comment
Share on other sites

Link to post
Share on other sites

A double is a floating point number (like 1.1234) while an int is just a whole number (like 1).

 

You can keep mid and SIZE as doubles, but when you iterate with them you can cast them into ints like: (int)mid. So for example in this line instead of: if (a[mid]>x), you can do: if (a[(int)mid]>x).

i9-11900K @ 5.1ghz;   RTX 3090 FE @ 2ghz;   ROG Maximus XII Hero;   4266mhz G.Skill 16GB DDR4;

Link to comment
Share on other sites

Link to post
Share on other sites

low, mid and high are all integers, because they're indexes

you're also creating an array of the primitive type 'double', but you fill it with Double objects, i guess it just dacays to primitive type but it's still not necessary

edit

SIZE has to be integer too, because it's a size

Link to comment
Share on other sites

Link to post
Share on other sites

 

Ok so I'm in a beginner java class and need help with the errors im getting in this program any help would be great.

 

CODE:

 
public class BinSearch
{
public static final int NOT_FOUND = -1;
public static double binarySearch(double[] a, double x)
{
double low=0;
double high= a.length-1;
double mid;
while (low<=high)
{
mid = (low+high)/2;
if (a[mid]>x)
low = mid+1;
if (a[mid]<x)
high = mid-1;
else
return mid;
}
return NOT_FOUND;
}
 
public static void main(String[] args)
{
double SIZE =43.5;
double[] a = new double;
for (double i=0; i<SIZE;i++)
a = new Double(i * 2);
for (double i=0; i<Size*2; i++)
System.out.println("Found"+i+ "at"+binarySearch(a, new Double(i)));
}
}
 
 

The errors consist with a few Loss of precision and one cannot find symbol
 
Once again thanks for the help.
 

 

Use the code tags ffs.

Link to comment
Share on other sites

Link to post
Share on other sites

Thanks for the help guys you explained a lot, I contacted my instructor this morning and he help me figure everything out. once again thanks 

Processor-Intel - Core i9-7900X 3.3GHz , Motherboard- Gigabyte - X299 AORUS Gaming 7, RAM- G.Skill - TridentZ RGB 32GB (4 x 8GB) DDR4-4000 Memory, GPU-Zotac - GeForce GTX 1080 Ti 11GB AMP Extreme,  Case- Phanteks - Enthoo Evolv ATX Glass, Storage- Samsung - 960 EVO 250GB M.2-2280, Samsung 850 Evo 250GB SSD, Samsung 850 EVO 1TB SSD, Toshiba 4TB (PH3400U) , PSU- SeaSonic 1200W Platinum, Cooling- NZXT - Kraken X62 Rev 2, Sound-Sennheiser - HD 598SE, SURE - SM7B, OS- Windows 10 Pro.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×