Пример #1
0
        public static IList <T?> SortByBubble <T>(this IList <T?> listaParaOrdenar, bool ordenAscendente = true) where T : struct, IComparable
        {
            //codigo de internet adaptado :)
            //Todos los derechos//http://www.c-sharpcorner.com/UploadFile/3d39b4/bubble-sort-in-C-Sharp/
            int  orden = ordenAscendente ? (int)Gabriel.Cat.S.Utilitats.CompareTo.Inferior : (int)Gabriel.Cat.S.Utilitats.CompareTo.Superior;
            bool flag  = true;
            T?   temp;
            int  numLength = listaParaOrdenar.Count;

            //sorting an array
            for (int i = 1; (i <= (numLength - 1)) && flag; i++)
            {
                flag = false;
                for (int j = 0, k; j < (numLength - 1); j++)
                {
                    k = j + 1;
                    if (ExtensionIComparable.CompareTo(listaParaOrdenar[k], listaParaOrdenar[j]) == orden)
                    {
                        temp = listaParaOrdenar[j];
                        listaParaOrdenar[j] = listaParaOrdenar[k];
                        listaParaOrdenar[k] = temp;
                        flag = true;
                    }
                }
            }
            return(listaParaOrdenar);
        }
        public static int CompareTo <T>(this Nullable <T> left, Nullable <T> right) where T : struct, IComparable
        {
            const int IGUALES  = 0;
            const int INFERIOR = -1;

            return(!left.HasValue && !right.HasValue? IGUALES : left.HasValue?ExtensionIComparable.CompareTo(left.Value, right.Value) : INFERIOR);
        }
Пример #3
0
        private static IList <T?> ISortByQuickSort <T>(IList <T?> elements, int left, int right, bool ordenAscendente) where T : struct, IComparable
        {
            //algoritmo sacado se internet
            //todos los derechos son de http://snipd.net/quicksort-in-c

            const int IGUALES = 0;
            int       i = left, j = right;
            T?        pivot;

            if (ordenAscendente)
            {
                if (right >= 0)
                {
                    pivot = elements[(left + right) / 2];

                    while (i <= j)
                    {
                        while (ExtensionIComparable.CompareTo(elements[i], pivot) < IGUALES)
                        {
                            i++;
                        }

                        while (ExtensionIComparable.CompareTo(elements[j], pivot) > IGUALES)
                        {
                            j--;
                        }
                        if (i <= j)
                        {
                            // Swap
                            elements.Swap(i, j);

                            i++;
                            j--;
                        }
                    }



                    // Recursive calls
                    if (left < j)
                    {
                        ISortByQuickSort(elements, left, j, ordenAscendente);
                    }

                    if (i < right)
                    {
                        ISortByQuickSort(elements, i, right, ordenAscendente);
                    }
                }
            }
            else
            {
                //se que no es optimo pero asi hay menos código :3
                SortByQuickSort(elements).Invertir();
            }

            return(elements);
        }
Пример #4
0
        public static bool Contains <T>(this IList <T> list, T element) where T : IComparable
        {
            const int IGUALS   = (int)Gabriel.Cat.S.Utilitats.CompareTo.Iguals;
            bool      contains = false;

            for (int i = 0; i < list.Count && !contains; i++)
            {
                contains = ExtensionIComparable.CompareTo(list[i], element) == IGUALS;
            }
            return(contains);
        }
Пример #5
0
        public static int BinarySearch <T>(this IList <T> list, T value) where T : IComparable
        {//source https://stackoverflow.com/questions/8067643/binary-search-of-a-sorted-array
            const int IGUALES = 0;
            const int MINIMO  = 2;
            int       pos     = -1;
            int       compareTo;

            bool found = false;
            int  first = 0, last = list.Count - 1, mid = list.Count / 2;

            list.SortByQuickSort(false);
            //for a sorted array with descending values
            if (list.Count > MINIMO)
            {
                while (!found && first <= last)
                {
                    mid       = (first + last) / 2;
                    compareTo = ExtensionIComparable.CompareTo(list[mid], value);
                    if (IGUALES < compareTo)
                    {
                        first = mid + 1;
                    }

                    if (IGUALES > compareTo)
                    {
                        last = mid - 1;
                    }

                    else
                    {
                        // You need to stop here once found or it's an infinite loop once it finds it.
                        found = true;
                        pos   = mid;
                    }
                }
            }
            else
            {
                for (int i = 0; i < list.Count && !found; i++)
                {
                    found = list[i].CompareTo(value) == IGUALES;
                    if (found)
                    {
                        pos = i;
                    }
                }
            }

            return(pos);
        }