public void BinarySearchTiny() { int[] expected = { 5, -1, 0, -1, 4, 5, 15, 14, 1, 0, 9, 13, -1, 10, 15, 13, 13, 12 }; int[] whiteList; // read the integers from a file and sort them using (In inWhiteList = new In("Algs4-Data\\TinyW.txt")) { whiteList = inWhiteList.ReadAllInts(); } Array.Sort(whiteList); // read integer keys from a file; search in whitelist using (In inKeys = new In("Algs4-Data\\TinyT.txt")) { int expectedIndex = 0; while (!inKeys.IsEmpty() && expected.Length > expectedIndex) { int key = inKeys.ReadInt(); int position = BinarySearch.Rank(key, whiteList); Assert.AreEqual(expected[expectedIndex++], position); } } }
public void Run() { Console.WriteLine("Choose file:"); // Prompt Console.WriteLine("1 - tinyT.txt"); // Prompt Console.WriteLine("2 - tinyW.txt"); // Prompt Console.WriteLine("3 - largeT.txt"); // Prompt Console.WriteLine("4 - largeW.txt"); // Prompt Console.WriteLine("or quit"); // Prompt var fileNumber = Console.ReadLine(); var fieName = string.Empty; switch (fileNumber) { case "1": fieName = "tinyT.txt"; break; case "2": fieName = "tinyW.txt"; break; case "3": fieName = "largeT.txt"; break; case "4": fieName = "largeW.txt"; break; case "quit": return; default: return; } var @in = new In($"Files\\BinarySearch\\{fieName}"); @in.ReadAllInts(); }
public void Run() { Console.WriteLine("Choose file:"); // Prompt Console.WriteLine("1 - tinyT.txt"); // Prompt Console.WriteLine("2 - tinyW.txt"); // Prompt Console.WriteLine("3 - largeT.txt"); // Prompt Console.WriteLine("4 - largeW.txt"); // Prompt Console.WriteLine("or quit"); // Prompt var fileNumber = Console.ReadLine(); var fieName = string.Empty; switch (fileNumber) { case "1": fieName = "tinyT.txt"; break; case "2": fieName = "tinyW.txt"; break; case "3": fieName = "largeT.txt"; break; case "4": fieName = "largeW.txt"; break; case "quit": return; default: return; } var @in = new In(string.Format("Files\\BinarySearch\\{0}", fieName)); var whitelist = @in.ReadAllInts(); // sort the array Array.Sort(whitelist); // Loop indefinitely // read integer key from standard input; print if not in whitelist while (true) { Console.WriteLine("Enter input:"); // Prompt var line = Console.ReadLine(); // Get string from user if (string.IsNullOrWhiteSpace(line)) { Console.WriteLine("input is empty"); continue; } if (line == "quit") // Check quit { break; } int key; try { key = Int32.Parse(line); } catch (Exception ex) { Console.WriteLine(ex.Message); continue; } var rank = Core.BinarySearch.BinarySearch.Rank(key, whitelist); Console.WriteLine("key: {0}, rank: {1}", key, rank); } }