public class SortPoints { public static void main(String[] args) { String string1 = args[0]; // first p String[] firstSplit = string1.split(";"); String[][] secondSplit = new String[firstSplit.length][2]; for (int i = 0; i < firstSplit.length; i++) { secondSplit[i] = firstSplit[i].split(","); } int[][] first = new int[0][0]; int[][] second = new int[0][0]; int[][] mainArray = new int[firstSplit.length][2]; for (int i = 0; i < firstSplit.length; i++) { mainArray[i][0] = Integer.parseInt(secondSplit[i][0]); mainArray[i][1] = Integer.parseInt(secondSplit[i][1]); } String string2 = args[1]; // second p String[] splitString2 = string2.split(","); String string3 = args[2]; // third p String[] splitString3 = string3.split(","); int x1 = Integer.parseInt(splitString2[0]); // x of first point int y1 = Integer.parseInt(splitString2[1]); // y of first point int x2 = Integer.parseInt(splitString3[0]); // x of second point int y2 = Integer.parseInt(splitString3[1]); // y of second point for (int i = 0; i < firstSplit.length; i++) { if (distance(mainArray[i][0], mainArray[i][1], x1, y1) <= distance(mainArray[i][0], mainArray[i][1], x2, y2)) { first = addToMassive(first, mainArray[i][0], mainArray[i][1]); } else { second = addToMassive(second, mainArray[i][0], mainArray[i][1]); } } sort(first); sort(second); for (int i = 0; i < first.length; i++) System.out.print(first[i][0] + "," + first[i][1] + " "); System.out.println(""); for (int i = 0; i < second.length; i++) System.out.print(second[i][0] + "," + second[i][1] + " "); } public static double distance(int x1, int x2, int y1, int y2) { double dist = Math.sqrt(Math.pow((x1 - y1), 2) + Math.pow((x2 - y2), 2)); return dist; } public static int[][] addToMassive(int[][] array, int x, int y ) { int[][] newFirst = new int[array.length + 1][2]; for (int i = 0; i < array.length; i++) { newFirst[i][0] = array[i][0]; newFirst[i][1] = array[i][1]; } newFirst[newFirst.length - 1][0] = x; newFirst[newFirst.length - 1][1] = y; return newFirst; } public static void sort(int[][] a) { for (int i = 1; i < a.length; i++) { insert(a, i); } } private static void insert(int[][] a, int index) { for (int i = index; i > 0 && a[i - 1][0] > a[i][0]; i--) { int tmp = a[i - 1][0]; a[i - 1][0] = a[i][0]; a[i][0] = tmp; } } } 

You need to write a program that takes 3 parameters via the command line. The first parameter: this is a sheet consisting of some points for example 81.71; 89.43; -92.64; -28, -34; -2.60; -11.58; 97, -84; -37, 19; -56, -51; -35, -60 (each point has x and y coordinates for the example x = 81 y = 71) The second and third parameters: these are some points for example 30.73 and 22, - 22

The program must calculate the distance between each of the points of the sheet, that is, 81.71; 89.43; -92.64 .... and points 30.73 and 22, -22 and sort the points from the sheet into two arrays, depending on which of the two points is closer to the point from the sheet, and then sort the points in the arrays from the x-coordinate from the lesser to the greater, if the two points have x-coordinates, then y-coordinate. An example of what the program should produce for a given sheet and two points: -92.64 -11.58 -2.60 81.71 89.43 -56, -51 -37.19 -35, -60 -28, - 34 97, -84

I wrote the program like this, but in the end I cannot sort the points in the arrays. And it gives me:

 -92,71 -11,43 -2,64 81,60 89,58 -56,-34 -37,-84 -35,19 -28,-51 97,-60 

    1 answer 1

    In the spirit of strange writing, you need to add to the insert method

     int tmp1 = a[i - 1][1]; a[i - 1][1] = a[i][1]; a[i][1] = tmp1; 

    You change x [0], but forgot to change y [1].