?
Solved

Nearest neighbour algorithm

Posted on 2002-07-28
5
Medium Priority
?
390 Views
Last Modified: 2010-03-31
Hi,

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??

Guster

0
Comment
Question by:Guster
[X]
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
5 Comments
 
LVL 1

Expert Comment

by:klf
ID: 7183509
sort the differences in descending order.
0
 
LVL 1

Accepted Solution

by:
PapaDanielle earned 150 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 ...
0
 

Author Comment

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

Author Comment

by:Guster
ID: 7183934
I think I got it
0
 
LVL 1

Expert Comment

by:PapaDanielle
ID: 7183982
OK. Glad to be of help.

Feel free to pass on any other prob.

Cheers

0

Featured Post

Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

Question has a verified solution.

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

Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month11 days, 7 hours left to enroll

752 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