public bool Contains(Object obj) { ClubMember cm = obj as ClubMember; Node temp = new Node(); temp = Head; while (temp.Next != null) { if (cm.Equals(temp.Data)) { return(true); } temp = temp.Next; } return(false); }
static void SearchBST(BinarySearchTree <ClubMember> Tree) { Random rnd = new Random(); ClubMember[] clubmemberstofind = new ClubMember[] { CMFactory.GetClubMember(), CMFactory.GetClubMember(), CMFactory.GetClubMember() }; for (int i = 0; i < 1000; i++) { foreach (var item in clubmemberstofind) { Tree.Find(item); } } }
static ClubMember[] InsertionSort(ClubMember[] arr) { for (int i = 1; i < arr.Length; i++) { int j = i; while ((j > 0) && (arr[j].Nr < arr[j - 1].Nr)) { int k = j - 1; ClubMember temp = arr[k]; arr[k] = arr[j]; arr[j] = temp; j--; } } return(arr); }
public int IndexOf(object obj) { ClubMember cm = obj as ClubMember; Node temp = new Node(); int counter = 0; temp = Head; while (temp.Next != null) { counter++; if (cm.Equals(temp.Data)) { return(counter); } temp = temp.Next; } return(-1); }
public void SortByID() { ClubMember cm1 = new ClubMember(); ClubMember cm2 = new ClubMember(); for (Node ptr = Head.Next; ptr.Next != null; ptr = ptr.Next) { for (Node newptr = ptr.Next; newptr != null; newptr = newptr.Next) { cm1 = (ClubMember)ptr.Data; cm2 = (ClubMember)newptr.Data; if (cm1.Nr > cm2.Nr) { Swap(ptr, newptr); } } } }
public static void SearchBinary(ClubMember[] arr) { Random rnd = new Random(); ClubMember[] clubmemberstofind = new ClubMember[] { (ClubMember)arr[rnd.Next(0, arr.Length)], (ClubMember)arr[rnd.Next(0, arr.Length)], (ClubMember)arr[rnd.Next(0, arr.Length)] }; for (int i = 0; i < 1000; i++) { foreach (var item in clubmemberstofind) { int low = 0; int high = arr.Length - 1; int middle = (low + high + 1) / 2; int location = -1; do { for (int j = 0; j < middle; j++) { if (item.Nr == arr[middle].Nr) { location = middle; } else if (item.Nr < arr[middle].Nr) { high = middle - 1; } else { low = middle + 1; } } middle = (low + high + 1) / 2; }while ((low <= high) && (location == -1)); // Console.WriteLine("tofind " + item.ToString() +" "+ arr[location].ToString()); } } }
static void SearchHashADT(HashADT hash) { Random rnd = new Random(); ClubMember[] clubmemberstofind = new ClubMember[] { CMFactory.GetClubMember(), CMFactory.GetClubMember(), CMFactory.GetClubMember() }; for (int i = 0; i < 1000; i++) { foreach (var item in clubmemberstofind) { if (hash.IndexInUse(hash.GetIndex(item)) == false) { return; } hash.GetElement(hash.Search(item)); } } }
public static void SearchLinear(IComparable[] arr) { Random rnd = new Random(); ClubMember cm1 = (ClubMember)arr[rnd.Next(0, arr.Length)]; ClubMember cm2 = (ClubMember)arr[rnd.Next(0, arr.Length)]; ClubMember cm3 = (ClubMember)arr[rnd.Next(0, arr.Length)]; ClubMember[] cmarr = new ClubMember[] { cm1, cm2, cm3 }; for (int i = 0; i < 1000; i++) { foreach (var item in cmarr) { for (int ii = 0; ii < arr.Length; ii++) { arr[ii].Equals(item); } } } }
static void Main(string[] args) { #region Setup int smallSize = 1000; int largeSize = smallSize * 10; ClubMember[] smallLinear = new ClubMember[smallSize]; ClubMember[] largeLinear = new ClubMember[largeSize]; ClubMember[] smallBinary = new ClubMember[smallSize]; ClubMember[] largeBinary = new ClubMember[largeSize]; BinarySearchTree <ClubMember> SmallCmTree = new BinarySearchTree <ClubMember>(); BinarySearchTree <ClubMember> LargeCmTree = new BinarySearchTree <ClubMember>(); HashADT smallHashADT = new HashADT(smallSize); HashADT largeHashADT = new HashADT(largeSize); for (int i = 0; i < smallSize; i++) { smallLinear[i] = CMFactory.GetClubMember(); smallBinary[i] = CMFactory.GetClubMember(); SmallCmTree.Insert(CMFactory.GetClubMember()); smallHashADT.Insert(CMFactory.GetClubMember()); } for (int i = 0; i < largeSize; i++) { largeLinear[i] = CMFactory.GetClubMember(); largeBinary[i] = CMFactory.GetClubMember(); LargeCmTree.Insert(CMFactory.GetClubMember()); largeHashADT.Insert(CMFactory.GetClubMember()); } #endregion System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch(); sw.Start(); #region Linear search Console.WriteLine("Linear Searching:"); Console.WriteLine("Searching small array:"); SearchLinear(smallLinear); Console.WriteLine(sw.Elapsed); Console.WriteLine("Searching large array:"); sw.Restart(); SearchLinear(largeLinear); Console.WriteLine(sw.Elapsed); #endregion #region Binary Search Console.WriteLine(); Console.WriteLine("Sorting small array:"); sw.Restart(); InsertionSort(smallBinary); Console.WriteLine(sw.Elapsed); Console.WriteLine(); Console.WriteLine("Searching small binary"); sw.Restart(); SearchBinary(smallBinary); Console.WriteLine(sw.Elapsed); Console.WriteLine(); Console.WriteLine("Sorting large array:"); sw.Restart(); InsertionSort(largeBinary); Console.WriteLine(sw.Elapsed); Console.WriteLine(); Console.WriteLine("Searching large binary"); sw.Restart(); SearchBinary(largeBinary); Console.WriteLine(sw.Elapsed); #endregion #region Binary Search Tree Console.WriteLine(); Console.WriteLine("Searching small binary tree"); sw.Restart(); SearchBST(SmallCmTree); Console.WriteLine(sw.Elapsed); Console.WriteLine(); Console.WriteLine("Searching large binary tree"); sw.Restart(); SearchBST(LargeCmTree); Console.WriteLine(sw.Elapsed); #endregion #region HashADTs Console.WriteLine("HashADT"); Console.WriteLine("Searching small Hash ADT"); sw.Restart(); SearchHashADT(smallHashADT); Console.WriteLine(sw.Elapsed); Console.WriteLine(); Console.WriteLine("Searching large Hash ADT"); sw.Restart(); SearchHashADT(largeHashADT); Console.WriteLine(sw.Elapsed); #endregion sw.Stop(); Console.ReadKey(); }