Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main;
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- Scanner scanner = new Scanner(System.in);
- System.out.print("Länge: ");
- final int length = scanner.nextInt();
- System.out.print("Suche: ");
- final int search = scanner.nextInt();
- scanner.close();
- int a[] = new int[length];
- for (int i = 0; i < length; i++) {
- a[i] = i;
- }
- try {
- //iterativ(a, search);
- recrusiv(a, search);
- System.out.println("FOUND");
- } catch (NumberNotFoundException e) {
- System.out.println("Die gegebene Nummer konnte nicht gefunden werden.");
- }
- }
- public static void iterativ(int[] a, int search) throws NumberNotFoundException {
- boolean found = false;
- int start = 0;
- int end = a.length - 1;
- while(!found) {
- int mid = ((end - start) / 2) + start;
- if (a[mid] >= search) {
- end = mid;
- } else {
- start = mid;
- }
- found = a[start] == search || a[end] == search;
- if (end - start >= -1 && end - start <= 1) {
- throw new NumberNotFoundException();
- }
- }
- }
- public static boolean recrusiv(int a[], int search) throws NumberNotFoundException {
- return recrusiv(a, search, 0, a.length - 1);
- }
- public static boolean recrusiv(int a[], int search, int start, int end) throws NumberNotFoundException {
- if (end - start >= -1 && end - start <= 1) {
- throw new NumberNotFoundException();
- }
- final int mid = start + ((end - start) / 2);
- if (a[mid] >= search) {
- end = mid;
- } else {
- start = mid;
- }
- return (a[start] == search || a[end] == search) ? true : recrusiv(a, search, start, end);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement