• 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) 
  • 相关阅读:
    第十九节 集群模式内各节点的通信和文件拷贝
    第十八节 虚拟机克隆后ip修改
    WAF 与 RASP 的安装使用大比拼!
    不改变中间层,如何玩转 .NET 的远程处理功能?
    什么是实时应用程序自我保护(RASP)?
    拒绝「技术栈」选择恐惧症
    为什么Nagios会那么吵?你又能做些什么呢?(1)
    Java 应用发布后,需要关注的7个性能指标
    玩转AWS CloudWatch微信告警
    日均百万 PV 的站点如何做性能监测?试试「3M口罩」!
  • 原文地址:https://www.cnblogs.com/joycelee/p/4516179.html
Copyright © 2020-2023  润新知