❔ Learning binary searching

i linked the txt file for my entire page the part im stuck on is
//
// Part B-2: FindHero
// The method should have a string parameter for the name of the hero to find.
// Call the BinarySearch method from part A-3.
// Print the result.
// If the found index is -1,
// print "<insert heroName> is not found"
// otherwise
// print "<insert heroName> was found at index <insert found index>"
//

public void FindHero(String hero)
{
BinarySearch(_heroes);
}
//
// Part B-2: FindHero
// The method should have a string parameter for the name of the hero to find.
// Call the BinarySearch method from part A-3.
// Print the result.
// If the found index is -1,
// print "<insert heroName> is not found"
// otherwise
// print "<insert heroName> was found at index <insert found index>"
//

public void FindHero(String hero)
{
BinarySearch(_heroes);
}
6 Replies
BI || CrescentThief
unsure how to binary search through this
Kouhai
Kouhai15mo ago
First binary search works on sorted arrays so make sure your items are sorted Second binary search works by comparing the value we are searching for with the middle value for the current subset If our value is large than the current mid, we go to the right and get the middle of it If it's smaller we go to the left and get the middle of it We keep doing that till either the value is found or there are no more subsets to check
BI || CrescentThief
i think my bunary is setup worng but dont know how to fix it as well i want to search through a hero array with a string but unsure how to use less than or greater than when comparing element locations
Kouhai
Kouhai15mo ago
You don't compare element location, you compare the items and then move accordingly
JakenVeina
JakenVeina15mo ago
well, that's the point that Kouhai made. You don't compare locations, you compare elements so, A) your elements need to be comparable, and B) they need to be already-sorted, within the array under search
Accord
Accord15mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.