Results 1 to 3 of 3
Thread: Custom binary search problem
- 09-16-2010, 09:04 PM #1
Member
- Join Date
- Sep 2010
- Posts
- 2
- Rep Power
- 0
Custom binary search problem
Hey all, been having a bit of trouble with this piece of my code for a binary search. It's part of a bigger project, but basically what it needs to do is look for a "Creature" by the creature's name.
Java Code:import java.util.ArrayList; public class BinarySearch { public static Creature binarySearch(ArrayList<Creature> list, String creatureName) { Creature retVal = null; int size = list.size(); if (size > 0) { System.out.println(list); int index = list.size() / 2; Creature mid = (Creature) list.get(index); String midName = mid.name; System.out.print("midName: " + midName + " || mid: " + mid); System.out.println(" || creatureName: " + creatureName); ArrayList<Creature> temp = new ArrayList(); if (midName.compareTo(creatureName) < 0) { for (int i = index; i < size; i++) { temp.add((Creature) list.get(i)); } binarySearch(temp, creatureName); } else if (midName.compareTo(creatureName) > 0) { for (int i = 0; i < index; i++) { temp.add(list.get(i)); } binarySearch(temp, creatureName); } else if (midName.equalsIgnoreCase(creatureName)) { System.out.println("Match Found"); retVal = mid; System.out.println("retVal = " + retVal); } } System.out.println("retVal = " + retVal); return retVal; } }
Here is some example output:
Java Code:[Ema, NPC, a student who always writes Java projects on time, Jeremy, NPC, a student who writes Java projects in the very last moment, Lewell, NPC, a student who doesn't even know what a Java project is, Lily, Animal, a black and white cat, Lucy, Animal, a pink pig, Martina, NPC, a student who doesn't write Java projects at all, Mary, NPC, a tall woman, Mutsu, Animal, a black-spotted pig, Peter, Animal, a brown dog, Squash, PC, one who doesn't leave the animals and NPC-s alone] midName: Martina || mid: Martina, NPC, a student who doesn't write Java projects at all || creatureName: Lily [Ema, NPC, a student who always writes Java projects on time, Jeremy, NPC, a student who writes Java projects in the very last moment, Lewell, NPC, a student who doesn't even know what a Java project is, Lily, Animal, a black and white cat, Lucy, Animal, a pink pig] midName: Lewell || mid: Lewell, NPC, a student who doesn't even know what a Java project is || creatureName: Lily [Lewell, NPC, a student who doesn't even know what a Java project is, Lily, Animal, a black and white cat, Lucy, Animal, a pink pig] midName: Lily || mid: Lily, Animal, a black and white cat || creatureName: Lily Match Found retVal = Lily, Animal, a black and white cat retVal = Lily, Animal, a black and white cat retVal = null retVal = null
- 09-16-2010, 09:18 PM #2
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 14,422
- Blog Entries
- 7
- Rep Power
- 29
You binarySearch( ... ) method seems to return a Creature when it found it; you're also calling this method recursively but you ignore its return value; so make those recursive invocations:
Java Code:return binarySearch(temp, creatureName);
kind regards,
Jos
- 09-16-2010, 09:53 PM #3
Member
- Join Date
- Sep 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
Binary search tree search method
By chopo1980 in forum New To JavaReplies: 2Last Post: 12-10-2009, 02:42 AM -
Binary Search Problem
By sharpnova in forum New To JavaReplies: 2Last Post: 02-19-2009, 11:22 AM -
Help. Binary Search Problem
By Krooger in forum Advanced JavaReplies: 1Last Post: 11-03-2008, 07:19 AM -
binary search
By tranceluv in forum New To JavaReplies: 10Last Post: 01-14-2008, 08:13 PM -
problem with recursive binary search program
By imran_khan in forum New To JavaReplies: 3Last Post: 08-02-2007, 04:08 PM
Bookmarks