static void Main() { // Shuffle var collectionToShuffle = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; var shuffler = new Shuffler <int>(); Console.WriteLine($"Collection before shuffle -> {string.Join(", ", collectionToShuffle)}"); shuffler.Shuffle(collectionToShuffle); Console.WriteLine($"Collection after shuffle -> {string.Join(", ", collectionToShuffle)}"); var collectionToSearch = new List <int> { -36, 1, 16, 42, 278, 301 }; Console.WriteLine(); Console.WriteLine($"Collection to search -> [{string.Join(", ", collectionToSearch)}]"); // Binary Search var binarySearcher = new BinarySearcher <int>(); int binarySearcherIndex = binarySearcher.Search(301, collectionToSearch); Console.WriteLine($"Binary search, searching for {301} -> index[{binarySearcherIndex}]"); // Linear Search var linearSearcher = new LinearSearcher <int>(); int linearSearcherIndex = linearSearcher.Search(42, collectionToSearch); Console.WriteLine($"Linear search, searching for {42} -> index[{linearSearcherIndex}]"); }
public static void FindIndex_ArrayEmpty_MinusOneReturned([Random(100)] int itemToSearch) { // Arrange var searcher = new LinearSearcher <int>(); var inputArray = new int[0]; // Act var actualIndex = searcher.FindIndex(inputArray, itemToSearch); // Assert Assert.AreEqual(-1, actualIndex); }
public void ShouldReturnCorrectIndexIfElementDoesNotExists() { var collection = new List <int> { -1, 10, 98, 103, 1067, 1069 }; var binarySearcher = new LinearSearcher <int>(); int actualIndex = binarySearcher.Search(1, collection); int expectedIndex = -1; Assert.AreEqual(expectedIndex, actualIndex); }
public static void FindIndex_MissingItem_MinusOneRetured([Random(0, 1000, 100)] int maxLimit, [Random(-100, 1100, 10)] int missingItem) { // Arrange var searcher = new LinearSearcher <int>(); var random = Randomizer.CreateRandomizer(); var inputArray = Enumerable.Range(0, maxLimit).Select(x => random.Next(0, 1000)).Where(x => x != missingItem).OrderBy(x => x).ToArray(); // Act var actualIndex = searcher.FindIndex(inputArray, missingItem); // Assert Assert.AreEqual(-1, actualIndex); }
public static void FindIndex_CorrectItemInList_CorrectIndex([Random(1, 1000, 100)] int maxLimit) { //Arrange var searcher = new LinearSearcher <int>(); var randomizer = Randomizer.CreateRandomizer(); var inputArray = Enumerable.Range(0, maxLimit).Select(t => randomizer.Next(0, 1000)).OrderBy(t => t).ToArray(); var selectedIndex = randomizer.Next(0, maxLimit); //Act var actualIndex = searcher.FindIndex(inputArray, inputArray[selectedIndex]); //Assert Assert.AreEqual(inputArray[selectedIndex], inputArray[actualIndex]); }
public static void FindIndex_NullArray_MinusOneReturned([Random(100)] int itemToSearch) { //Arrange var searcher = new LinearSearcher <int>(); var randomizer = Randomizer.CreateRandomizer(); int[] inputArray = null; //Act var actualIndex = searcher.FindIndex(inputArray, itemToSearch); //Assert Assert.AreEqual(-1, actualIndex); }