Exemplo n.º 1
0
        public static int BinarySearchCat <T>(IList <T> list, T targetValue, int leftIndex, int rightIndex,
                                              IndexOccurType indexOccurType        = IndexOccurType.Any_Index,
                                              IList <Comparison <T> > compareRules = null)
        {
            int            resultIndex    = -1;
            ListSortedType listSortedType = list.GetListSortedType(compareRules);

            while (leftIndex <= rightIndex)
            {
                int middleIndex   = (leftIndex + rightIndex) / 2;
                T   middleValue   = list[middleIndex];
                int compareResult = CompareUtil.CompareWithRules(targetValue, middleValue, compareRules);
                if (compareResult == 0)                 //相等的情况
                {
                    switch (indexOccurType)
                    {
                    case IndexOccurType.Any_Index:
                        return(middleIndex);

                    default:
                        resultIndex = middleIndex;
                        _BinarySearchSetLeftRightIndex(ref leftIndex, ref rightIndex, compareResult,
                                                       indexOccurType, listSortedType);
                        break;
                    }
                }
                else
                {
                    _BinarySearchSetLeftRightIndex(ref leftIndex, ref rightIndex, compareResult, indexOccurType,
                                                   listSortedType);
                }
            }

            return(resultIndex);
        }
Exemplo n.º 2
0
        public static ListSortedType GetListSortedType <T>(this IList <T> self, IList <Comparison <T> > compareRules)
        {
            T firstValue = self[0];
            T lastValue  = self[self.Count - 1];

            return(CompareUtil.CompareWithRules(firstValue, lastValue, compareRules) <= 0
                                ? ListSortedType.Increase
                                : ListSortedType.Decrease);
        }
Exemplo n.º 3
0
 private int CompareWithRules(T data1, T data2)
 {
     return(CompareUtil.CompareWithRules(data1, data2, this.compareRules));
 }
Exemplo n.º 4
0
 public static void MergeSortWithCompareRules <T>(IList <T> list, IList <Comparison <T> > compareRules)
 {
     MergeSort(list, (a, b) => CompareUtil.CompareWithRules(a, b, compareRules) < 0);
 }
Exemplo n.º 5
0
 public static void BubbleSortWithCompareRules(IList list, IList <Comparison <object> > compareRules)
 {
     BubbleSort(list, (a, b) => CompareUtil.CompareWithRules(a, b, compareRules) < 0);
 }