• Find the Smallest K Elements in an Array


    Given one point P0 on a 2-dimension space. There are n other points on the same space.

    Try to find K points which are most closed to P0.

    hint:

      Part of Quick Sort. Just sort the useful part of the array.

      1 public class findKPoints {
      2     class Point {
      3         int x;
      4         int y;
      5         public Point(int x, int y) {
      6             this.x = x;
      7             this.y = y;
      8         }
      9     }
     10     
     11     Point p0;
     12     ArrayList<Point> points = new ArrayList<Point>();
     13     int k;
     14 
     15     public void setSpace() {
     16         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
     17         System.out.println("Please set the position of p0 in format 'x,y':");
     18         try {
     19             String[] str = reader.readLine().split(",");
     20             this.p0 = new Point(Integer.parseInt(str[0]), Integer.parseInt(str[1]));
     21         } catch (IOException e) {
     22             e.printStackTrace();
     23             System.exit(1);
     24         }
     25         System.out.println("Please set the position of other points in format 'x,y', type 'OK' when done:");
     26         try {
     27             String tmp = reader.readLine();
     28             while (!tmp.equals("OK")) {
     29                 String[] str = tmp.split(",");
     30                 this.points.add(new Point(Integer.parseInt(str[0]), Integer.parseInt(str[1])));
     31                 tmp = reader.readLine();
     32             }
     33             if (this.points.size()==0) {
     34                 System.err.print("no other points on the space");
     35             }
     36         } catch (IOException e) {
     37             e.printStackTrace();
     38         }
     39         System.out.println("Please give the number of closest points you want to find:");
     40         try {
     41             this.k = Integer.parseInt(reader.readLine());
     42             if (k > points.size()) {
     43                 System.err.println("not enough points");
     44                 System.exit(1);
     45             }
     46         } catch (Exception e) {
     47             e.printStackTrace();
     48         }
     49     }
     50     
     51     public void find() {
     52         int[][] dist = new int[points.size()][2];
     53         for (int i = 0; i < points.size(); i++) {
     54             dist[i][0] = calculateDis(p0, points.get(i));
     55             dist[i][1] = i;
     56         }
     57         sort(dist, k, 0, dist.length-1);
     58         for (int i = 0; i < k; i++) {
     59             int p = dist[i][1];
     60             System.out.print("("+points.get(p).x+", "+points.get(p).y+") ");
     61         }
     62         System.out.println();
     63     }
     64     
     65     public void sort(int[][] dist, int k, int start, int end) {
     66         int dis = dist[start][0];
     67         int p = dist[start][1];
     68         int i = start, j = end;
     69         while (i < j) {
     70             while (i < j && dist[j][0] >= dis) {
     71                 j--;
     72             }
     73             dist[i][0] = dist[j][0]; 
     74             dist[i][1] = dist[j][1];
     75             while (i<j && dist[i][0] < dis) {
     76                 i++;
     77             }
     78             dist[j][0] = dist[i][0]; 
     79             dist[j][1] = dist[i][1];
     80         }
     81         dist[i][0] = dis; 
     82         dist[i][1] = p;
     83         if (i-start == k-1) {
     84             return;
     85         } else if (i-start > k-1) {
     86             sort(dist, k, start, i);
     87         } else {
     88             sort(dist, k-(i-start+1), i+1, end);
     89         }
     90     }
     91     
     92     public int calculateDis(Point p1, Point p2) {
     93         return ((p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y));
     94     }
     95     
     96     public static void main(String[] args) {
     97         findKPoints kp = new findKPoints();
     98         kp.setSpace();
     99         kp.find();
    100     }
    101 }

    The execute of the program

    Please set the position of p0 in format 'x,y':
    0,0
    Please set the position of other points in format 'x,y', type 'OK' when done:
    1,1
    1,3
    -1,0
    1,2
    4,4
    2,1
    0,2
    OK
    Please give the number of closest points you want to find:
    5
    (-1, 0) (1, 1) (0, 2) (1, 2) (2, 1) 
  • 相关阅读:
    用Java来获取访问者真实的IP地址
    springMVC 配置多个视图解析器
    java 内部类(inner class)详解
    安装mysql步骤
    Jquery 获取父页面下指定iframe里的指定元素
    Oracle 中文排序
    Oracle update时做表关联
    Jquery IE8兼容性
    JQuery 动态创建表单,并自动提交
    JQuery 使用.show()和.hide()做的可爱动画
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516179.html
Copyright © 2020-2023  润新知