/// <summary> /// Input: the array needs to be already sorted /// Unsing a recursive function is searching for the element /// </summary> /// <typeparam name="T">Generic type</typeparam> /// <param name="array">Array given by the user/program</param> /// <param name="element">The element to be found</param> /// <param name="start">Where the search starts</param> /// <param name="end">Where the search ends</param> /// <returns></returns> private int Search <T>(GArrayList <T> array, T element, double start, double end) where T : IComparable { // If the starting point is bigger then the ending point or the ending point is smaller then the starting point // There is no element to be found if (start <= end) { int m = (int)Math.Round((start + end) / 2); // Here is calculated the middle index of the array if (array.Get(m).Equals(element)) //If the middle element of the array is the element that we are searching for then we found it { return(m); } else if (array.Get(m).CompareTo(element) < 0) //If not then is checking if the element is on the right side of the array { start = (double)(m + 1); return(Search(array, element, start, end)); // splits the array in half and searches in the right part } else //if not { end = (double)(m - 1); return(Search(array, element, start, end)); // splits the array in half and searches in the left part } } else // if there is no element found display { return(-1); } }
public GArrayList <T> NormalListBubbleSorting <T>(GArrayList <T> array) where T : IComparable { //gets the length of the array int Length = array.Size(); //a boolean so that the switch in the while loop would work Boolean sw = true; //a while loop to loop the for loop until theres nothing to sort anymore while (sw) { sw = false; for (int inner = 0; inner < Length - 1; inner++) { //this will compare the objects if (array.Get(inner).CompareTo(array.Get(inner + 1)) > 0) { //this T will hold the object we are using to compare T Swap = array.Get(inner); //this will change the place of the object that is being used to compare with the one that it is compared with array.Replace(inner, array.Get(inner + 1)); //this will change the place of the object to the place of the object that we are using to compare it to array.Replace(inner + 1, Swap); sw = true; } } } //returns the array return(array); }
public GArrayList <T> InsertionListSorting <T>(GArrayList <T> array) where T : IComparable { //this for loop runs the while loop as many time as there is objects in the array for (int counter = 0; counter < array.Size() - 1; counter++) { //index so that we can compare the objects in the array int index = counter + 1; //the while loop so that we can compare the all objects in the array while (index > 0) { //this if compares the object in the array and changes the places if needed if (array.Get(index - 1).CompareTo(array.Get(index)) > 0) { //hold the object for the time T Swap = array.Get(index - 1); //changes the placeing of the object we use to compare array.Insert(index, array.Get(index + 1)); //changes the place of the object with we put on hold just now array.Insert(index, Swap); } //lowers the index so this does not become an infinte loop index--; } } //returns the array return(array); }
//Removing the first element of the queue public T Remove() { T element = queue.Get(0); if (size > 0) { queue.Remove(0); size -= 1; } return(element); }
public Item <T> Remove() { Item <T> item = priorityQueue.Get(0); if (size > 0) { priorityQueue.Remove(0); size -= 1; } return(item); }
//Removing the top element of the stack public T Pop() { T element = stack.Get(size - 1); if (size > 0) { stack.Remove(size - 1); size -= 1; } else { size = 0; } return(element); }
/// <summary> /// this is doing a smart bubble sort which will sort an array by comparing /// the element by element sorting them until there is nothing to be sort /// but everytime when it's done with inner loop /// it will make the array shorter by 1 /// </summary> /// <typeparam name="T">gerneric type</typeparam> /// <param name="array">an array</param> /// <returns>the sorted array</returns> public GArrayList <T> BubbleSorting <T>(GArrayList <T> array) where T : IComparable { //gets the length of the array int Length = array.Size(); //the outer for loop is here to see how many times the inner for loop needs to run everytime the outer loop runs it will take away one number from the end of the array for (int outer = Length; outer >= 1; outer--) { //this inner for loop will run until the outer for loop stops so if(inner is smaller the outer it will stop running) for (int inner = 0; inner < outer - 1; inner++) { //this will compare the objects if (array.Get(inner) != null && array.Get(inner + 1) != null) { if (array.Get(inner).CompareTo(array.Get(inner + 1)) > 0) { //this T will hold the object we are using to compare T Swap = array.Get(inner); //this will change the place of the object that is being used to compare with the one that it is compared with array.Replace(inner, array.Get(inner + 1)); //this will change the place of the object to the place of the object that we are using to compare it to array.Replace(inner + 1, Swap); } } } } //returns the array return(array); }
/// <summary> /// Sequnetial Search is an algorithm that search element by element in an array /// till finds the desired element /// </summary> /// <typeparam name="T"> Generic type</typeparam> /// <param name="array"> Array given by user/program</param> /// <param name="element"> The element to be found</param> /// <returns></returns> public Boolean sequentialSearch <T>(GArrayList <T> array, T element) where T : IComparable { Boolean sw = true; // switch to stop the loop from running int size = array.Size(); // geting the size of the array for (int i = 0; i < size && sw; i++) //looping through the array { if (array.Get(i).Equals(element)) //checking if is the element { sw = false; // if yes then stop the loop return(true); // return true } } return(false); // after finishing the loop the element wasn't detected return false }
private BinaryTreeNode <T> getParent(int i) { return(tree.Get(i)); }