Java Array Sort sort(double[] coords1, int length1, double[] coords2, int length2, int[] array)

Here you can find the source of sort(double[] coords1, int length1, double[] coords2, int length2, int[] array)

Description

sort

License

Apache License

Declaration

private static void sort(double[] coords1, int length1,
            double[] coords2, int length2, int[] array) 

Method Source Code

//package com.java2s;
/*/*from w w  w .j a va  2 s .c o  m*/
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You under the Apache License, Version 2.0
 *  (the "License"); you may not use this file except in compliance with
 *  the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

public class Main {
    private static void sort(double[] coords1, int length1,
            double[] coords2, int length2, int[] array) {
        int temp;
        int length = length1 + length2;
        double x1, y1, x2, y2;

        for (int i = 1; i < length; i++) {
            if (array[i - 1] < length1) {
                x1 = coords1[2 * array[i - 1]];
                y1 = coords1[2 * array[i - 1] + 1];
            } else {
                x1 = coords2[2 * (array[i - 1] - length1)];
                y1 = coords2[2 * (array[i - 1] - length1) + 1];
            }
            if (array[i] < length1) {
                x2 = coords1[2 * array[i]];
                y2 = coords1[2 * array[i] + 1];
            } else {
                x2 = coords2[2 * (array[i] - length1)];
                y2 = coords2[2 * (array[i] - length1) + 1];
            }
            int j = i;
            while (j > 0 && compare(x1, y1, x2, y2) <= 0) {
                temp = array[j];
                array[j] = array[j - 1];
                array[j - 1] = temp;
                j--;
                if (j > 0) {
                    if (array[j - 1] < length1) {
                        x1 = coords1[2 * array[j - 1]];
                        y1 = coords1[2 * array[j - 1] + 1];
                    } else {
                        x1 = coords2[2 * (array[j - 1] - length1)];
                        y1 = coords2[2 * (array[j - 1] - length1) + 1];
                    }
                    if (array[j] < length1) {
                        x2 = coords1[2 * array[j]];
                        y2 = coords1[2 * array[j] + 1];
                    } else {
                        x2 = coords2[2 * (array[j] - length1)];
                        y2 = coords2[2 * (array[j] - length1) + 1];
                    }
                }
            }
        }
    }

    public static int compare(double x1, double y1, double x2, double y2) {

        if ((x1 < x2) || (x1 == x2 && y1 < y2)) {
            return 1;
        } else if (x1 == x2 && y1 == y2) {
            return 0;
        }

        return -1;
    }
}

Related

  1. sort( final Item[] values, final Item[] auxiliary, final int first, final int last)
  2. sort( float[] array)
  3. sort(byte[] b, int pos)
  4. sort(double s[], int idx[])
  5. sort(double[] a, int[] b)
  6. sort(double[] data)
  7. sort(double[] data, boolean desc)
  8. sort(Double[] inArray)
  9. sort(double[] vector, boolean ascending)