public void SearchLinear(IComparable[] arr) { Stopwatch stopwatch = Stopwatch.StartNew(); bool check; ClubMember clubMember1 = (ClubMember)arr[rnd.Next(0, arr.Length - 1)]; ClubMember clubMember2 = (ClubMember)arr[rnd.Next(0, arr.Length - 1)]; ClubMember clubMember3 = (ClubMember)arr[rnd.Next(0, arr.Length - 1)]; ClubMember found1 = null; ClubMember found2 = null; ClubMember found3 = null; Console.WriteLine(clubMember1.ToString()); Console.WriteLine(clubMember2.ToString()); Console.WriteLine(clubMember3.ToString()); for (int i = 0; i < 1000; i++) { check = true; int j = 0; while (check && j < arr.Length) { if (clubMember1.Equals(arr[j])) { check = false; found1 = (ClubMember)arr[j]; } j++; } check = true; j = 0; while (check && j < arr.Length) { if (clubMember2.Equals(arr[j])) { check = false; found2 = (ClubMember)arr[j]; } j++; } check = true; j = 0; while (check && j < arr.Length) { if (clubMember3.Equals(arr[j])) { check = false; found3 = (ClubMember)arr[j]; } j++; } } stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms"); Console.WriteLine(found1.ToString()); Console.WriteLine(found2.ToString()); Console.WriteLine(found3.ToString() + "\n"); }
private void Exercise6() { Console.WriteLine("Exercise 6"); ClubMember clubMember1 = new ClubMember(0, "Matt", "Peterson", 18); ClubMember clubMember2 = new ClubMember(1, "Matt", "Perry", 26); ClubMember clubMember3 = new ClubMember(2, "Anthony", "Peterson", 52); ClubMember clubMember4 = new ClubMember(3, "Carmelo", "Anthony", 30); BST.Insert(clubMember1); BST.Insert(clubMember2); BST.Insert(clubMember3); BST.Insert(clubMember4); Console.WriteLine(clubMember2.ToString() + " " + BST.Search(clubMember2).ToString()); Console.ReadKey(); }
public void SearchBinary(IComparable[] arr) { Stopwatch stopwatch = Stopwatch.StartNew(); int middle; int min; int max; ClubMember clubMember1 = (ClubMember)arr[rnd.Next(0, arr.Length - 1)]; ClubMember clubMember2 = (ClubMember)arr[rnd.Next(0, arr.Length - 1)]; ClubMember clubMember3 = (ClubMember)arr[rnd.Next(0, arr.Length - 1)]; ClubMember found1 = null; ClubMember found2 = null; ClubMember found3 = null; Console.WriteLine(clubMember1.ToString()); Console.WriteLine(clubMember2.ToString()); Console.WriteLine(clubMember3.ToString()); for (int i = 0; i < 1000; i++) { middle = arr.Length / 2; min = 0; max = arr.Length; while (found1 == null) { if (clubMember1.CompareTo(arr[middle]) == 1) { min = middle; middle = middle + ((max - middle) / 2); } else if (clubMember1.CompareTo(arr[middle]) == -1) { max = middle; middle = min + ((middle - min) / 2); } else { found1 = (ClubMember)arr[middle]; } } middle = arr.Length / 2; min = 0; max = arr.Length; while (found2 == null) { if (clubMember2.CompareTo(arr[middle]) == 1) { min = middle; middle = middle + ((max - middle) / 2); } else if (clubMember2.CompareTo(arr[middle]) == -1) { max = middle; middle = min + ((middle - min) / 2); } else { found2 = (ClubMember)arr[middle]; } } middle = arr.Length / 2; min = 0; max = arr.Length; while (found3 == null) { if (clubMember3.CompareTo(arr[middle]) == 1) { min = middle; middle = middle + ((max - middle) / 2); } else if (clubMember3.CompareTo(arr[middle]) == -1) { max = middle; middle = min + ((middle - min) / 2); } else { found3 = (ClubMember)arr[middle]; } } } stopwatch.Stop(); Console.WriteLine(stopwatch.ElapsedMilliseconds + "ms"); Console.WriteLine(found1.ToString()); Console.WriteLine(found2.ToString()); Console.WriteLine(found3.ToString() + "\n"); }