1
1
/*
2
2
* 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 - [email protected]
3
+ * Copyright (C) 2018-2025 Tilman Neumann - [email protected]
4
4
*
5
5
* This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License
6
6
* as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
13
13
*/
14
14
package de .tilman_neumann .jml ;
15
15
16
+ import java .util .Arrays ;
17
+
16
18
import de .tilman_neumann .util .Ensure ;
17
19
18
20
/**
@@ -34,7 +36,7 @@ public class BinarySearch {
34
36
* @param x
35
37
* @return the insert position
36
38
*/
37
- public int getInsertPosition (int [] array , int maxIndex , int x ) {
39
+ public int getInsertPosition_v1 (int [] array , int maxIndex , int x ) {
38
40
if (maxIndex <=0 || array [maxIndex -1 ] <= x ) return maxIndex ;
39
41
int left = 0 ;
40
42
int right = maxIndex -1 ;
@@ -56,6 +58,26 @@ public int getInsertPosition(int[] array, int maxIndex, int x) {
56
58
return left ;
57
59
}
58
60
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
+
59
81
/**
60
82
* Find the insert position for x into array given that array is sorted bottom-up.
61
83
*
@@ -68,7 +90,7 @@ public int getInsertPosition(int[] array, int maxIndex, int x) {
68
90
* @param x
69
91
* @return the insert position
70
92
*/
71
- public int getInsertPosition (byte [] array , int maxIndex , int x ) {
93
+ public int getInsertPosition_v1 (byte [] array , int maxIndex , byte x ) {
72
94
if (maxIndex <=0 || array [maxIndex -1 ] <= x ) return maxIndex ;
73
95
int left = 0 ;
74
96
int right = maxIndex -1 ;
@@ -89,4 +111,24 @@ public int getInsertPosition(byte[] array, int maxIndex, int x) {
89
111
}
90
112
return left ;
91
113
}
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
+ }
92
134
}
0 commit comments