< prev index next >

modules/javafx.graphics/src/main/java/com/sun/marlin/MergeSort.java

Print this page


   1 /*
   2  * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  44     static void mergeSortNoCopy(final int[] x, final int[] y,
  45                                 final int[] auxX, final int[] auxY,
  46                                 final int toIndex,
  47                                 final int insertionSortIndex)
  48     {
  49         if ((toIndex > x.length) || (toIndex > y.length)
  50                 || (toIndex > auxX.length) || (toIndex > auxY.length)) {
  51             // explicit check to avoid bound checks within hot loops (below):
  52             throw new ArrayIndexOutOfBoundsException("bad arguments: toIndex="
  53                                                      + toIndex);
  54         }
  55 
  56         // sort second part only using merge / insertion sort
  57         // in auxiliary storage (auxX/auxY)
  58         mergeSort(x, y, x, auxX, y, auxY, insertionSortIndex, toIndex);
  59 
  60         // final pass to merge both
  61         // Merge sorted parts (auxX/auxY) into x/y arrays
  62         if ((insertionSortIndex == 0)
  63             || (auxX[insertionSortIndex - 1] <= auxX[insertionSortIndex])) {
  64 //            System.out.println("mergeSortNoCopy: ordered");
  65             // 34 occurences
  66             // no initial left part or both sublists (auxX, auxY) are sorted:
  67             // copy back data into (x, y):
  68             System.arraycopy(auxX, 0, x, 0, toIndex);
  69             System.arraycopy(auxY, 0, y, 0, toIndex);
  70             return;
  71         }
  72 
  73         for (int i = 0, p = 0, q = insertionSortIndex; i < toIndex; i++) {
  74             if ((q >= toIndex) || ((p < insertionSortIndex)
  75                                    && (auxX[p] <= auxX[q]))) {
  76                 x[i] = auxX[p];
  77                 y[i] = auxY[p];
  78                 p++;
  79             } else {
  80                 x[i] = auxX[q];
  81                 y[i] = auxY[q];
  82                 q++;
  83             }
  84         }


 118                         break;
 119                     }
 120                 }
 121                 dstX[j + 1] = x;
 122                 dstY[j + 1] = y;
 123             }
 124             return;
 125         }
 126 
 127         // Recursively sort halves of dest into src
 128 
 129         // note: use signed shift (not >>>) for performance
 130         // as indices are small enough to exceed Integer.MAX_VALUE
 131         final int mid = (low + high) >> 1;
 132 
 133         mergeSort(refX, refY, dstX, srcX, dstY, srcY, low, mid);
 134         mergeSort(refX, refY, dstX, srcX, dstY, srcY, mid, high);
 135 
 136         // If arrays are inverted ie all(A) > all(B) do swap A and B to dst
 137         if (srcX[high - 1] <= srcX[low]) {
 138 //            System.out.println("mergeSort: inverse ordered");
 139             // 1561 occurences
 140             final int left = mid - low;
 141             final int right = high - mid;
 142             final int off = (left != right) ? 1 : 0;
 143             // swap parts:
 144             System.arraycopy(srcX, low, dstX, mid + off, left);
 145             System.arraycopy(srcX, mid, dstX, low, right);
 146             System.arraycopy(srcY, low, dstY, mid + off, left);
 147             System.arraycopy(srcY, mid, dstY, low, right);
 148             return;
 149         }
 150 
 151         // If arrays are already sorted, just copy from src to dest.  This is an
 152         // optimization that results in faster sorts for nearly ordered lists.
 153         if (srcX[mid - 1] <= srcX[mid]) {
 154 //            System.out.println("mergeSort: ordered");
 155             // 14 occurences
 156             System.arraycopy(srcX, low, dstX, low, length);
 157             System.arraycopy(srcY, low, dstY, low, length);
 158             return;
 159         }
 160 
 161         // Merge sorted halves (now in src) into dest
 162         for (int i = low, p = low, q = mid; i < high; i++) {
 163             if ((q >= high) || ((p < mid) && (srcX[p] <= srcX[q]))) {
 164                 dstX[i] = srcX[p];
 165                 dstY[i] = srcY[p];
 166                 p++;
 167             } else {
 168                 dstX[i] = srcX[q];
 169                 dstY[i] = srcY[q];
 170                 q++;
 171             }
 172         }
 173     }
 174 
   1 /*
   2  * Copyright (c) 2009, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any


  44     static void mergeSortNoCopy(final int[] x, final int[] y,
  45                                 final int[] auxX, final int[] auxY,
  46                                 final int toIndex,
  47                                 final int insertionSortIndex)
  48     {
  49         if ((toIndex > x.length) || (toIndex > y.length)
  50                 || (toIndex > auxX.length) || (toIndex > auxY.length)) {
  51             // explicit check to avoid bound checks within hot loops (below):
  52             throw new ArrayIndexOutOfBoundsException("bad arguments: toIndex="
  53                                                      + toIndex);
  54         }
  55 
  56         // sort second part only using merge / insertion sort
  57         // in auxiliary storage (auxX/auxY)
  58         mergeSort(x, y, x, auxX, y, auxY, insertionSortIndex, toIndex);
  59 
  60         // final pass to merge both
  61         // Merge sorted parts (auxX/auxY) into x/y arrays
  62         if ((insertionSortIndex == 0)
  63             || (auxX[insertionSortIndex - 1] <= auxX[insertionSortIndex])) {

  64             // 34 occurences
  65             // no initial left part or both sublists (auxX, auxY) are sorted:
  66             // copy back data into (x, y):
  67             System.arraycopy(auxX, 0, x, 0, toIndex);
  68             System.arraycopy(auxY, 0, y, 0, toIndex);
  69             return;
  70         }
  71 
  72         for (int i = 0, p = 0, q = insertionSortIndex; i < toIndex; i++) {
  73             if ((q >= toIndex) || ((p < insertionSortIndex)
  74                                    && (auxX[p] <= auxX[q]))) {
  75                 x[i] = auxX[p];
  76                 y[i] = auxY[p];
  77                 p++;
  78             } else {
  79                 x[i] = auxX[q];
  80                 y[i] = auxY[q];
  81                 q++;
  82             }
  83         }


 117                         break;
 118                     }
 119                 }
 120                 dstX[j + 1] = x;
 121                 dstY[j + 1] = y;
 122             }
 123             return;
 124         }
 125 
 126         // Recursively sort halves of dest into src
 127 
 128         // note: use signed shift (not >>>) for performance
 129         // as indices are small enough to exceed Integer.MAX_VALUE
 130         final int mid = (low + high) >> 1;
 131 
 132         mergeSort(refX, refY, dstX, srcX, dstY, srcY, low, mid);
 133         mergeSort(refX, refY, dstX, srcX, dstY, srcY, mid, high);
 134 
 135         // If arrays are inverted ie all(A) > all(B) do swap A and B to dst
 136         if (srcX[high - 1] <= srcX[low]) {

 137             // 1561 occurences
 138             final int left = mid - low;
 139             final int right = high - mid;
 140             final int off = (left != right) ? 1 : 0;
 141             // swap parts:
 142             System.arraycopy(srcX, low, dstX, mid + off, left);
 143             System.arraycopy(srcX, mid, dstX, low, right);
 144             System.arraycopy(srcY, low, dstY, mid + off, left);
 145             System.arraycopy(srcY, mid, dstY, low, right);
 146             return;
 147         }
 148 
 149         // If arrays are already sorted, just copy from src to dest.  This is an
 150         // optimization that results in faster sorts for nearly ordered lists.
 151         if (srcX[mid - 1] <= srcX[mid]) {

 152             // 14 occurences
 153             System.arraycopy(srcX, low, dstX, low, length);
 154             System.arraycopy(srcY, low, dstY, low, length);
 155             return;
 156         }
 157 
 158         // Merge sorted halves (now in src) into dest
 159         for (int i = low, p = low, q = mid; i < high; i++) {
 160             if ((q >= high) || ((p < mid) && (srcX[p] <= srcX[q]))) {
 161                 dstX[i] = srcX[p];
 162                 dstY[i] = srcY[p];
 163                 p++;
 164             } else {
 165                 dstX[i] = srcX[q];
 166                 dstY[i] = srcY[q];
 167                 q++;
 168             }
 169         }
 170     }
 171 
< prev index next >