int[] daten = new int[] { 5, 6, 8, 9, 9, 14, 17, 18, 30, 33, 38, 39, 52, 65, 69, 72, 78, 82, 83, 84, 86, 89, 95, 98, 99 }; int gesucht = 18; int linkeGrenze = 0; int rechteGrenze = daten.length - 1; boolean gefunden = false; int position = -1; int anzahlSchritte = 0; while (!gefunden && rechteGrenze > linkeGrenze) { anzahlSchritte++; int pivotIndex = linkeGrenze + (rechteGrenze - linkeGrenze) / 2; int pivotElement = daten[pivotIndex]; if (gesucht < pivotElement) { rechteGrenze = pivotIndex - 1; } else if (gesucht > pivotElement) { linkeGrenze = pivotIndex + 1; } else { gefunden = true; position = pivotIndex; } } if (!gefunden) { println("Das Element " + gesucht + " wurde nicht gefunden. Abbruch nach " + anzahlSchritte + " Schritten."); } else { println("Die Position von " + gesucht + " ist: " + position + ". Abbruch nach " + anzahlSchritte + " Schritten."); }