Nearest neighbour algorithm

Posted on 2002-07-28
Last Modified: 2010-03-31

I'm trying to write a 'nearest neighbour algorithm' which basically finds a certain number of values in an array that are the closest to a certain value. For example, if I have an array of values:

18, 23, 22, 25, 20, 19, 18, 19, 25

I want to get the 6 values that have the least difference between the value 20. So in this case it would be the values:

18, 18, 19, 19, 22, 23

The first thing I did was to get the differences between each value and 20:

public void kneighbour(int[] values) {
  int number = values.length;
  float one = -1;
  float middle;
  float[] diff = new float[values.length];
  for(int i = 0; i< number; i++) {
     diff[i] = values[number/2] - values[i];
     if(diff[i] < 0) {
     diff[i] = diff[i]*one;
    System.out.println("This difference is : " + diff[i]);

Once I have all the differences how would I go about getting the six least differences??


Question by:Guster
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2

Expert Comment

ID: 7183509
sort the differences in descending order.

Accepted Solution

PapaDanielle earned 50 total points
ID: 7183897
>>Once I have all the differences how would I go about
>> getting the six least differences??

sort the differences in ascending order , ie from least to greatest ...

Author Comment

ID: 7183927
how would I be able to relate those differences back to their original array values??

Author Comment

ID: 7183934
I think I got it

Expert Comment

ID: 7183982
OK. Glad to be of help.

Feel free to pass on any other prob.



Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
maven disable workspace resolution 1 39
American Express @Work site and Java 4 64
Java array 21 83
Java array sort 10 31
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question