This commit is contained in:
@@ -12,10 +12,14 @@ public class BinarySearch3637 {
|
|||||||
for (int i = 1; i < n; i++) {
|
for (int i = 1; i < n; i++) {
|
||||||
a.put(Integer.parseInt(args[i]));
|
a.put(Integer.parseInt(args[i]));
|
||||||
}
|
}
|
||||||
System.out.println(rightBound(x, a) - leftBound(x, a));
|
// System.out.println(rightBoundIterative(x, a) - leftBoundIterative(x, a));
|
||||||
|
System.out.println(
|
||||||
|
rightBoundRecursive(x, a, 0, a.getLength()) -
|
||||||
|
leftBoundRecursive(x, a, 0, a.getLength())
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int leftBound(int x, IntList a) {
|
static int leftBoundIterative(int x, IntList a) {
|
||||||
int low = 0,
|
int low = 0,
|
||||||
high = a.getLength() - 1;
|
high = a.getLength() - 1;
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
@@ -29,7 +33,7 @@ public class BinarySearch3637 {
|
|||||||
return high + 1;
|
return high + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int rightBound(int x, IntList a) {
|
static int rightBoundIterative(int x, IntList a) {
|
||||||
int low = 0,
|
int low = 0,
|
||||||
high = a.getLength() - 1;
|
high = a.getLength() - 1;
|
||||||
while (low <= high) {
|
while (low <= high) {
|
||||||
@@ -43,47 +47,31 @@ public class BinarySearch3637 {
|
|||||||
return high + 1;
|
return high + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int searchIterativeDecreasing(int x, IntList a) {
|
static int leftBoundRecursive(int x, IntList a, int low, int high) {
|
||||||
if (a.getLength() == 0) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
int low = 0,
|
|
||||||
high = a.getLength() - 1;
|
|
||||||
|
|
||||||
while (low <= high) {
|
|
||||||
int mid = low + (high - low) / 2;
|
|
||||||
|
|
||||||
if (a.get(mid) <= x) {
|
|
||||||
high = mid - 1;
|
|
||||||
} else {
|
|
||||||
low = mid + 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return low;
|
|
||||||
}
|
|
||||||
|
|
||||||
static int searchRecursive(int x, IntList a) {
|
|
||||||
return searchRecursiveHelper(x, a, 0, a.getLength() - 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int searchRecursiveHelper(
|
|
||||||
int x,
|
|
||||||
IntList a,
|
|
||||||
int low,
|
|
||||||
int high
|
|
||||||
) {
|
|
||||||
if (low > high) {
|
if (low > high) {
|
||||||
return low;
|
return high + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
int mid = low + (high - low) / 2;
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
if (a.get(mid) <= x) {
|
if (a.get(mid) > x) {
|
||||||
return searchRecursiveHelper(x, a, low, mid - 1);
|
return leftBoundRecursive(x, a, mid + 1, high);
|
||||||
} else {
|
} else {
|
||||||
return searchRecursiveHelper(x, a, mid + 1, high);
|
return leftBoundRecursive(x, a, low, mid - 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int rightBoundRecursive(int x, IntList a, int low, int high) {
|
||||||
|
if (low > high) {
|
||||||
|
return high + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int mid = low + (high - low) / 2;
|
||||||
|
|
||||||
|
if (a.get(mid) >= x) {
|
||||||
|
rightBoundRecursive(x, a, mid + 1, high);
|
||||||
|
} else {
|
||||||
|
rightBoundRecursive(x, a, low, mid - 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user