示例#1
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);
 }
示例#2
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);
        }
示例#3
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);
        }
示例#4
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
        }
        /// <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));
        }