first commit

This commit is contained in:
me
2026-04-08 21:25:17 +03:00
parent 3681b8eccd
commit 371b14c5e3
173 changed files with 14126 additions and 0 deletions

View File

@@ -0,0 +1,65 @@
package search;
/**
* @author Nikita Doschennikov (me@fymio.us)
*/
public class BinarySearch {
public static void main(String[] args) {
IntList a = new IntList();
int x = Integer.parseInt(args[0]);
int n = args.length;
for (int i = 1; i < n; i++) {
a.put(Integer.parseInt(args[i]));
}
System.out.println(searchRecursive(x, a));
// System.out.println(searchIterative(x, a));
}
static int searchIterative(int x, IntList a) {
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) {
return low;
}
int mid = low + (high - low) / 2;
if (a.get(mid) <= x) {
return searchRecursiveHelper(x, a, low, mid - 1);
} else {
return searchRecursiveHelper(x, a, mid + 1, high);
}
}
}

View File

@@ -0,0 +1,50 @@
package search;
/**
* @author Nikita Doschennikov (me@fymio.us)
*/
public class BinarySearch3233 {
public static void main(String[] args) {
IntList a = new IntList();
for (String arg : args) {
a.put(Integer.parseInt(arg));
}
// System.out.println(searchIterative(a));
System.out.println(searchRecursive(a, 0, a.getLength() - 1));
}
static int searchIterative(IntList a) {
int arrLength = a.getLength();
if (arrLength <= 1) return 0;
int low = 0, high = arrLength - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (a.get(mid) < a.get(arrLength - 1)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
static int searchRecursive(IntList a, int low, int high) {
if (low >= high) {
return low;
}
int mid = low + (high - low) / 2;
if (a.get(mid) < a.get(a.getLength() - 1)) {
return searchRecursive(a, low, mid);
} else {
return searchRecursive(a, mid + 1, high);
}
}
}

View File

@@ -0,0 +1,48 @@
package search;
/**
* @author Nikita Doschennikov (me@fymio.us)
*/
public class BinarySearch3435 {
public static void main(String[] args) {
IntList a = new IntList();
for (String arg : args) {
a.put(Integer.parseInt(arg));
}
// System.out.println(searchIterative(a));
System.out.println(searchRecursive(a, 0, a.getLength() - 1));
}
static int searchIterative(IntList a) {
int arrLength = a.getLength();
if (arrLength <= 1) return 0;
int low = 0, high = arrLength - 1;
while (low < high) {
int mid = low + (high - low) / 2;
if (a.get(mid) > a.get(arrLength - 1)) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
static int searchRecursive(IntList a, int low, int high) {
if (low >= high) {
return low;
}
int mid = low + (high - low) / 2;
if (a.get(mid) > a.get(a.getLength() - 1)) {
return searchRecursive(a, low, mid);
} else {
return searchRecursive(a, mid + 1, high);
}
}
}

View File

@@ -0,0 +1,77 @@
package search;
/**
* @author Nikita Doschennikov (me@fymio.us)
*/
public class BinarySearch3637 {
public static void main(String[] args) {
IntList a = new IntList();
int x = Integer.parseInt(args[0]);
int n = args.length;
for (int i = 1; i < n; i++) {
a.put(Integer.parseInt(args[i]));
}
// System.out.println(rightBoundIterative(x, a) - leftBoundIterative(x, a));
System.out.println(
rightBoundRecursive(x, a, 0, a.getLength() - 1) -
leftBoundRecursive(x, a, 0, a.getLength() - 1)
);
}
static int leftBoundIterative(int x, IntList a) {
int low = 0,
high = a.getLength() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a.get(mid) > x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high + 1;
}
static int rightBoundIterative(int x, IntList a) {
int low = 0,
high = a.getLength() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a.get(mid) >= x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high + 1;
}
static int leftBoundRecursive(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) {
return leftBoundRecursive(x, a, mid + 1, high);
} else {
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) {
return rightBoundRecursive(x, a, mid + 1, high);
} else {
return rightBoundRecursive(x, a, low, mid - 1);
}
}
}

View File

@@ -0,0 +1,77 @@
package search;
/**
* @author Nikita Doschennikov (me@fymio.us)
*/
public class BinarySearch3839 {
public static void main(String[] args) {
IntList a = new IntList();
int x = Integer.parseInt(args[0]);
int n = args.length;
for (int i = 1; i < n; i++) {
a.put(Integer.parseInt(args[i]));
}
int leftBound = leftBoundIterative(x, a);
int rightBound = rightBoundIterative(x, a);
int range = rightBound - leftBound;
System.out.println(leftBound + " " + range);
}
static int leftBoundIterative(int x, IntList a) {
int low = 0,
high = a.getLength() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a.get(mid) > x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high + 1;
}
static int rightBoundIterative(int x, IntList a) {
int low = 0,
high = a.getLength() - 1;
while (low <= high) {
int mid = low + (high - low) / 2;
if (a.get(mid) >= x) {
low = mid + 1;
} else {
high = mid - 1;
}
}
return high + 1;
}
static int leftBoundRecursive(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) {
return leftBoundRecursive(x, a, mid + 1, high);
} else {
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) {
return rightBoundRecursive(x, a, mid + 1, high);
} else {
return rightBoundRecursive(x, a, low, mid - 1);
}
}
}

View File

@@ -0,0 +1,180 @@
package search;
import base.MainChecker;
import base.Runner;
import base.Selector;
import base.TestCounter;
import java.util.Arrays;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.IntStream;
import java.util.stream.Stream;
import static java.util.stream.IntStream.range;
/**
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
public final class BinarySearchTest {
public static final int[] SIZES = {5, 4, 2, 1, 10, 50, 100, 200, 300};
public static final int[] VALUES = new int[]{5, 4, 2, 1, 0, 10, 100, Integer.MAX_VALUE / 2};
private BinarySearchTest() {
}
// === Base
/* package-private */ static int base(final int c, final int x, final int[] a) {
return IntStream.range(0, a.length).filter(i -> Integer.compare(a[i], x) != c).findFirst().orElse(a.length);
}
// === 3637
private static int count(final int c, final int x, final int[] a) {
return (int) range(0, a.length).filter(i -> a[i] == x).count();
}
// === 3839
private static String span(final int c, final int x, final int[] a) {
final int begin = range(0, a.length).filter(i -> a[i] == x || Integer.compare(x, a[i]) == c).findFirst().orElse(a.length);
final long length = range(0, a.length).filter(i -> a[i] == x).count();
return begin + " " + length;
}
// === 3435
private static Consumer<TestCounter> shift(final String name, final Kind kind) {
final Sampler sampler = new Sampler(kind, false, false);
return variant(name, variant -> {
for (final int s : SIZES) {
final int size = s > 3 * TestCounter.DENOMINATOR ? s / TestCounter.DENOMINATOR : s;
for (final int max : VALUES) {
final int[] a = sampler.sample(variant, size, max);
for (int k = 0; k < a.length; k++) {
variant.test(k, a);
final int last = a[a.length - 1];
System.arraycopy(a, 0, a, 1, a.length - 1);
a[0] = last;
}
}
}
});
}
// === Common code
public static final Selector SELECTOR = new Selector(BinarySearchTest.class)
.variant("Base", Solver.variant0("", Kind.DESC, BinarySearchTest::base))
.variant("3637", Solver.variant0("3637", Kind.DESC, BinarySearchTest::count))
.variant("3839", Solver.variant0("3839", Kind.DESC, BinarySearchTest::span))
.variant("3435", shift("3435", Kind.DESC))
.variant("3233", shift("3233", Kind.ASC))
;
public static void main(final String... args) {
SELECTOR.main(args);
}
/* package-private */ static Consumer<TestCounter> variant(final String name, final Consumer<Variant> variant) {
final String className = "BinarySearch" + name;
return counter -> variant.accept(new Variant(counter, new MainChecker(Runner.packages("search").args(className))));
}
/* package-private */ interface Solver {
static Consumer<TestCounter> variant0(final String name, final Kind kind, final Solver solver) {
return variant(name, kind, true, solver);
}
static Consumer<TestCounter> variant1(final String name, final Kind kind, final Solver solver) {
return variant(name, kind, false, solver);
}
private static Consumer<TestCounter> variant(final String name, final Kind kind, final boolean empty, final Solver solver) {
final Sampler sampler = new Sampler(kind, true, true);
return BinarySearchTest.variant(name, vrt -> {
if (empty) {
solver.test(kind, vrt);
}
solver.test(kind, vrt, 0);
for (final int s1 : SIZES) {
final int size = s1 > 3 * TestCounter.DENOMINATOR ? s1 / TestCounter.DENOMINATOR : s1;
for (final int max : VALUES) {
solver.test(kind, vrt, sampler.sample(vrt, size, max));
}
}
});
}
private static int[] probes(final int[] a, final int limit) {
return Stream.of(
Arrays.stream(a),
IntStream.range(1, a.length).map(i -> (a[i - 1] + a[i]) / 2),
IntStream.of(
0, Integer.MIN_VALUE, Integer.MAX_VALUE,
a.length > 0 ? a[0] - 1 : -1,
a.length > 0 ? a[a.length - 1] + 1 : 1
)
)
.flatMapToInt(Function.identity())
.distinct()
.sorted()
.limit(limit)
.toArray();
}
Object solve(final int c, final int x, final int... a);
default void test(final Kind kind, final Variant variant, final int... a) {
test(kind, variant, a, Integer.MAX_VALUE);
}
default void test(final Kind kind, final Variant variant, final int[] a, final int limit) {
for (final int x : probes(a, limit)) {
variant.test(solve(kind.d, x, a), IntStream.concat(IntStream.of(x), Arrays.stream(a)));
}
}
}
public enum Kind {
ASC(-1), DESC(1);
public final int d;
Kind(final int d) {
this.d = d;
}
}
public record Variant(TestCounter counter, MainChecker checker) {
void test(final Object expected, final IntStream ints) {
final List<String> input = ints.mapToObj(Integer::toString).toList();
checker.testEquals(counter, input, List.of(expected.toString()));
}
public void test(final Object expected, final int[] a) {
test(expected, Arrays.stream(a));
}
}
public record Sampler(Kind kind, boolean dups, boolean zero) {
public int[] sample(final Variant variant, final int size, final int max) {
final IntStream sorted = variant.counter.random().getRandom().ints(zero ? size : Math.max(size, 1), -max, max + 1).sorted();
final int[] ints = (dups ? sorted : sorted.distinct()).toArray();
if (kind == Kind.DESC) {
final int sz = ints.length;
for (int i = 0; i < sz / 2; i++) {
final int t = ints[i];
ints[i] = ints[sz - i - 1];
ints[sz - i - 1] = t;
}
}
return ints;
}
}
}

48
java/search/IntList.java Normal file
View File

@@ -0,0 +1,48 @@
package search;
import java.util.Arrays;
/**
* @author Nikita Doschennikov (me@fymio.us)
*/
public class IntList {
protected int[] list = new int[8];
protected int idx = 0;
public IntList() {}
public IntList(int[] list) {
this.list = list;
}
public void put(int val) {
if (idx >= list.length) {
list = Arrays.copyOf(list, list.length * 2);
}
list[idx++] = val;
}
public int getLength() {
return idx;
}
public int get(int index) {
return list[index];
}
public String toString() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < idx; i++) {
if (i == idx - 1) {
sb.append(String.valueOf(list[i]) + "\n");
} else {
sb.append(String.valueOf(list[i]) + " ");
}
}
return sb.toString();
}
}

View File

@@ -0,0 +1,7 @@
/**
* Tests for <a href="https://www.kgeorgiy.info/courses/paradigms/homeworks.html#java-binary-search">Binary Search</a> homework
* of <a href="https://www.kgeorgiy.info/courses/paradigms/">Paradigms of Programming</a> course.
*
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
package search;