11/*
22 * java-math-library is a Java library focused on number theory, but not necessarily limited to it. It is based on the PSIQS 4.0 factoring project.
3- * Copyright (C) 2018-2024 Tilman Neumann - tilman.neumann@web.de
3+ * Copyright (C) 2018-2025 Tilman Neumann - tilman.neumann@web.de
44 *
55 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
66 * as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
1313 */
1414package de .tilman_neumann .jml ;
1515
16+ import java .util .Arrays ;
17+
1618import de .tilman_neumann .util .Ensure ;
1719
1820/**
@@ -34,7 +36,7 @@ public class BinarySearch {
3436 * @param x
3537 * @return the insert position
3638 */
37- public int getInsertPosition (int [] array , int maxIndex , int x ) {
39+ public int getInsertPosition_v1 (int [] array , int maxIndex , int x ) {
3840 if (maxIndex <=0 || array [maxIndex -1 ] <= x ) return maxIndex ;
3941 int left = 0 ;
4042 int right = maxIndex -1 ;
@@ -56,6 +58,26 @@ public int getInsertPosition(int[] array, int maxIndex, int x) {
5658 return left ;
5759 }
5860
61+ /**
62+ * Find the insert position for x into array given that array is sorted bottom-up.
63+ *
64+ * More precisely:
65+ * If array[maxIndex-1] > x, return the index of the first entry of array[0].. array[maxIndex-1] greater than x.
66+ * If array[maxIndex-1] <= x, return maxIndex.
67+ *
68+ * Faster version using Arrays.binarySearch().
69+ *
70+ * @param array
71+ * @param maxIndex the maximum index to consider, exclusive (may be smaller than the array size)
72+ * @param x
73+ * @return the insert position
74+ */
75+ public int getInsertPosition /*_v2*/ (int [] array , int maxIndex , int x ) {
76+ // see @returns in Arrays.binarySearch() javadoc
77+ int i = Arrays .binarySearch (array , 0 , maxIndex , x );
78+ return i >= 0 ? i + 1 : -i - 1 ;
79+ }
80+
5981 /**
6082 * Find the insert position for x into array given that array is sorted bottom-up.
6183 *
@@ -68,7 +90,7 @@ public int getInsertPosition(int[] array, int maxIndex, int x) {
6890 * @param x
6991 * @return the insert position
7092 */
71- public int getInsertPosition (byte [] array , int maxIndex , int x ) {
93+ public int getInsertPosition_v1 (byte [] array , int maxIndex , byte x ) {
7294 if (maxIndex <=0 || array [maxIndex -1 ] <= x ) return maxIndex ;
7395 int left = 0 ;
7496 int right = maxIndex -1 ;
@@ -89,4 +111,24 @@ public int getInsertPosition(byte[] array, int maxIndex, int x) {
89111 }
90112 return left ;
91113 }
114+
115+ /**
116+ * Find the insert position for x into array given that array is sorted bottom-up.
117+ *
118+ * More precisely:
119+ * If array[maxIndex-1] > x, return the index of the first entry of array[0].. array[maxIndex-1] greater than x.
120+ * If array[maxIndex-1] <= x, return maxIndex.
121+ *
122+ * Faster version using Arrays.binarySearch().
123+ *
124+ * @param array
125+ * @param maxIndex the maximum index to consider, exclusive (may be smaller than the array size)
126+ * @param x
127+ * @return the insert position
128+ */
129+ public int getInsertPosition /*_v2*/ (byte [] array , int maxIndex , byte x ) {
130+ // see @returns in Arrays.binarySearch() javadoc
131+ int i = Arrays .binarySearch (array , 0 , maxIndex , x );
132+ return i >= 0 ? i + 1 : -i - 1 ;
133+ }
92134}
0 commit comments