• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Nearest neighbour...How to find it?

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

Well i am trying to find out neastest two or three values which are closer to "str" value when compared with the rest of the values. All i want to know is what is the best possible way to get the closest match of query(str) from list of BigInteger. the Code is given Below


//Start Of Code

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.Vector;


class Testing1
{
public static void main(String args[])
{
String a1, a2, a3, a4, a5, str="";

a1 = "04545b969af70c70e6379239ea701801a1dfcf1c";
a2 = "356a192b7913b04c54574d18c28d46e6395428ab";
a3 = "5ee5c0b94c86b17b8a51dac910235945a700aaeb";
a4 = "86b17b8a51dac91026379239ea701801a8736497";
a5 = "701801a1dfcf1c4545b969a5ee5c0b94c3546356";

BigInteger n[] = new BigInteger[6];
BigInteger a,b;

int mode;

n[0] = new BigInteger(a1, 16);
n[1] = new BigInteger(a2, 16);
n[2] = new BigInteger(a3, 16);
n[3] = new BigInteger(a4, 16);
n[4] = new BigInteger(a5, 16);

try
{
BufferedReader userInput = new BufferedReader (new InputStreamReader(System.in));
System.out.print("\n\n\n\t\tEnter Qeuery string <> ");
str = userInput.readLine();

str = MySHA1.SHA1(str);
}
catch(Exception e)
{}



n[5] = new BigInteger(str, 16);

System.out.println("Hash Value of Query String is "+str+"\n\n\nBig Integer Value of String is "+n[5]);

for(int i=0; i<n.length-1; i++ ){
a = n[5].subtract(n[i]);
a = a.abs();
System.out.println("\n\nCalculation is = "+a);
//Here I want to comparison b/w a and n[i] want the 2 closest values from n[i]. How can i get those?
}
}
}

//End of Code

Best Regards.
Salman
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not sure, so don't blame me if its wrong, but have you tried a bubble sort or similar to sort the numbers into order, and then do a test to compare the numbers to see if they are nearest neighbours?

RC
 
Nothing up my sleeve ... and ... presto! A tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic