Friday, September 13, 2013

find closest pair


    static void findClosestPair(int[] arr) {
        int[] x = new int[]{1, 2, 4, 5, 6, 8, 9, 10, 12, 14};
        int[] y = new int[]{100, 98, 45, 63, 33, 96, 2, -10, -100, 400};
        float[] d = new float[10];
        float[] ang = new float[10];
        for (int i = 0; i < 10; i++) {
            d[i] = (float) Math.sqrt(x[i] * x[i] + y[i] * y[i]);
            ang[i] = y[i] / x[i];
        }
        for (int i = 1; i < d.length; i++) {
            float temp = d[i];
            int j = 0;
            while (j < i && d[i] > d[j]) {
                j++;
            }
            d[i] = d[j];
            d[j] = temp;
            float atemp = ang[i];
            ang[i] = ang[j];
            ang[j] = atemp;
        }
    }

No comments:

Post a Comment