private static void CompareLinearAndBinarySearch() { for (int arraySize = 1; arraySize < 25; arraySize++) { Console.WriteLine("CompareLinearAndBinarySearch. ArraySize: {0}", arraySize); Console.WriteLine("*******************************************************************"); int[] keys = new int[arraySize]; for (int i = 0; i < arraySize; i++) { keys[i] = i + 1; } Stopwatch sw = Stopwatch.StartNew(); long k = 0; for (int i = 0; i < 1000000; i++) { k += SearchHelpers.UpperBoundLinear(keys, arraySize, arraySize); } sw.Stop(); Console.WriteLine("K: {0}. UpperBoundLinear: {1} ms", k, sw.ElapsedMilliseconds); sw.Restart(); k = 0; for (int i = 0; i < 1000000; i++) { k += SearchHelpers.UpperBoundLinear4(keys, arraySize, arraySize); } sw.Stop(); Console.WriteLine("K: {0}. UpperBoundLinear4: {1} ms", k, sw.ElapsedMilliseconds); sw.Restart(); k = 0; for (int i = 0; i < 1000000; i++) { k += SearchHelpers.UpperBoundLinear8(keys, arraySize, arraySize); } sw.Stop(); Console.WriteLine("K: {0}. UpperBoundLinear8: {1} ms", k, sw.ElapsedMilliseconds); sw.Restart(); k = 0; for (int i = 0; i < 1000000; i++) { k += SearchHelpers.UpperBoundBinary(keys, arraySize, arraySize); } sw.Stop(); Console.WriteLine("K: {0}. UpperBoundBinary: {1} ms", k, sw.ElapsedMilliseconds); Console.WriteLine("*******************************************************************\r\n"); } }
public void UpperBoundBinary_Random() { for (int i = 1; i < 1000; i++) { var keys = GetSortedRandomKeys(i); for (int k = 0; k < keys.Length; k++) { Assert.AreEqual(SearchHelpers.UpperBoundLinear(keys, keys.Length, keys[k]), SearchHelpers.UpperBoundBinary(keys, keys.Length, keys[k])); Assert.AreEqual(SearchHelpers.UpperBoundLinear(keys, keys.Length, keys[k] + 1), SearchHelpers.UpperBoundBinary(keys, keys.Length, keys[k] + 1)); Assert.AreEqual(SearchHelpers.UpperBoundLinear(keys, keys.Length, keys[k] - 1), SearchHelpers.UpperBoundBinary(keys, keys.Length, keys[k] - 1)); } } }
public void UpperBoundBinary_Seq() { for (int i = 1; i < 1000; i++) { var keys = GenerateKeys(i); for (int k = 0; k < keys.Length; k++) { Assert.AreEqual(SearchHelpers.UpperBoundLinear(keys, keys.Length, keys[k]), SearchHelpers.UpperBoundBinary(keys, keys.Length, keys[k])); } } }