Informatiker Board (http://www.informatikerboard.de/board/index.php)
- Themengebiete (http://www.informatikerboard.de/board/board.php?boardid=1)
-- Praktische Informatik (http://www.informatikerboard.de/board/board.php?boardid=6)
--- Suche im Array (http://www.informatikerboard.de/board/thread.php?threadid=2085)
Geschrieben von neuling96 am 21.01.2015 um 16:39:
| Zitat: |
Original von neuling96
| code: |
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
|
public class Suche {
public static boolean suchebinaer(int a, int [] array){
if(array.length==1 && a != array[0]) {
return false;
}else {
int m= array.length/2;
if(array[m]==a){
return true;
}else {
if(array[m]>=a){
int[]arrayx=new int [m+1];
for(int i=0; i<=m; i++){
arrayx[i]=array[i];
}
return suchebinaer(a, arrayx);
}else {
int[]arrayx=new int [m];
for(int i=0; i<m; i++){
arrayx[i]=array[i+m];
}
return suchebinaer(a, arrayx);
}
}
}
}
}
|
|
l |
würdet man diesen code als rekursiv bezeichnen?
ich mein es "ruft" sich doch selbst wieder auf?

, also ja ?
Geschrieben von eulerscheZahl am 21.01.2015 um 16:41:
Ja.
Geschrieben von neuling96 am 21.01.2015 um 17:38:
. Falls nein, wird die Suche auf der
vorderen Halfte (falls der Suchwert kleiner ist) oder hinteren Halfte des Arrays wiederholt.
wenn ich dein code geanu ansehe, dann läuft es nicht nach diesen schema, oder ??
, aber ich bin mir nicht sicher
ich hab versucht dein code um zuschreiben
public class Suche {
private static boolean suchebinaer(int a, int[] array, int start, int end) {
if (start >= end) {
return array[start] == a;
}else{
int mitte = (start + end) / 2;
if (array[mitte] == a) {
return true;
}else{
if (array[mitte] > a){
return suchebinaer(a, array, start, mitte - 1);
}else{
return suchebinaer(a, array, mitte + 1, end);
}
}
}
}
public static boolean suchebinaer(int a, int[] array) {
if (array.length == 0) {
return false;
}else{
return suchebinaer(a, array, 0, array.length - 1);
}
}
}
Geschrieben von eulerscheZahl am 21.01.2015 um 17:43:
Nein, das was ich geschrieben habe, ist keine binäre Suche. Ich wollte dir nur zeigen, wie du das Kopieren des Array vermeiden kannst.
Sieht gut aus, wie du die Unterscheidung des zu durchsuchenden Intervalls eingebaut hast.
Forensoftware: Burning Board, entwickelt von WoltLab GmbH