示例#1
0
        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);
        }
 /// <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);
     }
 }
示例#3
0
 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);
 }
示例#4
0
        /// <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);
        }
 public BinaryTree()
 {
     tree      = new GArrayList <BinaryTreeNode <T> >();
     matrics   = new GArrayList <GArrayList <BinaryTreeNode <T> > >();
     child     = 0;
     parentPos = 0;
     root      = null;
 }
 public BucketHashing()
 {
     bucket = new GArrayList <T>();
     for (int i = 0; i < size; i++)
     {
         // bucket.Add(new GArrayList<T>());
     }
 }
示例#7
0
        /// <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
        }
 public Stack()
 {
     stack = new GArrayList <T>();
     size  = 0;
 }
        /// <summary>
        ///  This function is used for getting the array from the main program
        ///  Here is calculated the size of the array and passed forward to the function that does all the work Search
        /// </summary>
        /// <typeparam name="T">Generic type</typeparam>
        /// <param name="array">The array gived by the user needs to be already sorted</param>
        /// <param name="element">The element to be found</param>
        /// <returns></returns>

        public int binarySearch <T>(GArrayList <T> array, T element) where T : IComparable
        {
            double size = (double)array.Size() - 1;

            return(Search(array, element, 0, size));
        }
示例#10
0
 public Queue()
 {
     queue = new GArrayList <T>();
     size  = 0;
 }
 public HashTable()
 {
     hashTable = new GArrayList <Slot <T> >();
     size      = 0;
 }
 public PriorityQueue()
 {
     size          = 0;
     priorityQueue = new GArrayList <Item <T> >();
 }