//C# Binair Zoeken in Arrays Arrays public class BinarySearch { void Main() { int @base = 2; int count = 10; int upperbound = (count - 1); int[] numbers = new int[0]; int index; this.numbers(index) = ((index + 1) * @base); int number; upperbound = (count - 1); int lowerbound = 0; bool found = false; bool exhausted = false; for (; (found || exhausted); ) { index = ((lowerbound + upperbound) / 2); found = (number == this.numbers(index)); exhausted = (upperbound <= lowerbound); if (((found || exhausted) == false)) { if ((number > this.numbers(index))) { lowerbound = (index + 1); } else { upperbound = (index - 1); } } } if (found) { Console.Write(((number + " found at index ") + index)); } else { Console.Write((number + " not found")); } if (exhausted) { Console.Write(", search exhausted"); } else { Console.Write(", search not exhausted"); } Console.WriteLine((((", last searched " + lowerbound) + " to ") + upperbound)); Console.ReadLine(); } } public class BinarySearch { void Main() { int @base = 2; int count = 10; int upperbound = (count - 1); int[] numbers = new int[0]; int index; this.numbers(index) = ((index + 1) * @base); int number; index = Array.BinarySearch(numbers, number); bool found = (index > -1); if (found) { Console.WriteLine(((number + " found at index ") + index)); } else { Console.WriteLine((number + " not found")); } Console.ReadLine(); } } public class ExerciseTask { void Main() { int count = 3; string[] names; int[] zipCodes; Console.WriteLine("Zip Code ?"); string zipCode = Console.ReadLine(); Console.ReadLine(); } } public class ExerciseSolution { void Main() { int count = 3; string[] names; int[] zipCodes; Console.WriteLine("Zip Code ?"); string zipCode = Console.ReadLine(); bool found; bool exhausted; int lowerbound; int index; int upperbound = (count - 1); for (; (found || exhausted); ) { index = ((lowerbound + upperbound) / 2); found = (zipCode == this.zipCodes(index)); exhausted = (upperbound <= lowerbound); if (((found || exhausted) == false)) { if ((zipCode > this.zipCodes(index))) { lowerbound = (index + 1); } else { upperbound = (index - 1); } } } if (found) { Console.WriteLine(this.names(index)); } else { Console.WriteLine("City not found."); } Console.ReadLine(); } } //Bezoek www.vbvoorbeelden.be voor meer C# voorbeelden. //Copyright - De Wolf / vbvoorbeelden - 2003-2011 - Alle rechten voorbehouden.