Compare() public method

public Compare ( object x, object y ) : int
x object
y object
return int
Esempio n. 1
0
        public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
        {
            var comparer = new CustomComparer <T>(sortDirection, Comparer <T> .Default);

            var k = array.Length / 2;
            var j = 0;

            while (k >= 1)
            {
                for (int i = k; i < array.Length; i = i + k, j = j + k)
                {
                    if (comparer.Compare(array[i], array[j]) >= 0)
                    {
                        continue;
                    }

                    swap(array, i, j);

                    if (i <= k)
                    {
                        continue;
                    }

                    i -= k * 2;
                    j -= k * 2;
                }

                j  = 0;
                k /= 2;
            }

            return(array);
        }
Esempio n. 2
0
        public void PriorityQueueStabilityTest()
        {
            CustomComparer <int>     comparer = new CustomComparer <int>((a, b) => Comparer <int> .Default.Compare(a / 10, b / 10));
            PriorityQueue <int, int> queue    = new PriorityQueue <int, int>(comparer);
            List <int> list   = new List <int>();
            Random     random = new Random();

            for (int i = 0; i < 100; i++)
            {
                int n = random.Next(100);

                int j = 0;
                while (j < list.Count && comparer.Compare(list[j], n) != 1)
                {
                    j++;
                }

                list.Insert(j, n);
                queue.Enqueue(n, n);
            }

            for (int i = 0; i < list.Count; i++)
            {
                int n = queue.Dequeue();
                Assert.AreEqual(list[i], n);
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Merge two sorted arrays.
        /// </summary>
        private static void merge(Indexable <T> array, int leftStart, int middle, int rightEnd,
                                  CustomComparer <T> comparer)
        {
            var newLength = rightEnd - leftStart + 1;

            var result = new T[newLength];

            int i = leftStart, j = middle + 1, k = 0;

            //iteratively compare and pick min to result
            while (i <= middle && j <= rightEnd)
            {
                if (comparer.Compare(array[i], array[j]) < 0)
                {
                    result[k] = array[i];
                    i++;
                }
                else
                {
                    result[k] = array[j];
                    j++;
                }
                k++;
            }

            //copy left overs
            if (i <= middle)
            {
                for (var l = i; l <= middle; l++)
                {
                    result[k] = array[l];
                    k++;
                }
            }
            else
            {
                for (var l = j; l <= rightEnd; l++)
                {
                    result[k] = array[l];
                    k++;
                }
            }

            k = 0;
            //now write back result
            for (var g = leftStart; g <= rightEnd; g++)
            {
                array[g] = result[k];
                k++;
            }
        }
Esempio n. 4
0
            public int Compare(T x, T y)
            {
                if (_comparer != null && x != null && y != null)
                {
                    var result = _comparer.Compare(Property.GetValue(x), Property.GetValue(y));
                    if (Direction == ListSortDirection.Descending)
                    {
                        result = -result;
                    }

                    if (result != 0)
                    {
                        return(result);
                    }
                }

                return(_baseComparer?.Compare(x, y) ?? 0);
            }
Esempio n. 5
0
        /// <summary>
        /// Time complexity: O(n^2).
        /// </summary>
        public static Span <T> Sort(Span <T> array, SortDirection sortDirection = SortDirection.Ascending)
        {
            var comparer = new CustomComparer <T>(sortDirection, Comparer <T> .Default);

            for (int i = 0; i < array.Length; i++)
            {
                //select the smallest item in sub array and move it to front
                for (int j = i + 1; j < array.Length; j++)
                {
                    if (comparer.Compare(array[j], array[i]) >= 0)
                    {
                        continue;
                    }

                    var temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
            }

            return(array);
        }
Esempio n. 6
0
        private static void sort(Span <T> array, int startIndex, int endIndex, CustomComparer <T> comparer)
        {
            while (true)
            {
                //if only one element the do nothing
                if (startIndex < 0 || endIndex < 0 || endIndex - startIndex < 1)
                {
                    return;
                }

                //set the wall to the left most index
                var wall = startIndex;

                //pick last index element on array as comparison pivot
                var pivot = array[endIndex];

                //swap elements greater than pivot to the right side of wall
                //others will be on left
                for (var j = wall; j <= endIndex; j++)
                {
                    if (comparer.Compare(pivot, array[j]) <= 0 && j != endIndex)
                    {
                        continue;
                    }

                    var temp = array[wall];
                    array[wall] = array[j];
                    array[j]    = temp;
                    //increment to exclude the minimum element in subsequent comparisons
                    wall++;
                }

                //sort left
                sort(array, startIndex, wall - 2, comparer);
                //sort right
                startIndex = wall;
            }
        }
        /// <summary>
        /// Time complexity: O(n^2).
        /// </summary>
        public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
        {
            var comparer = new CustomComparer <T>(sortDirection, Comparer <T> .Default);

            for (int i = 0; i < array.Length - 1; i++)
            {
                for (int j = i + 1; j > 0; j--)
                {
                    if (comparer.Compare(array[j], array[j - 1]) < 0)
                    {
                        var temp = array[j - 1];
                        array[j - 1] = array[j];
                        array[j]     = temp;
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return(array);
        }
Esempio n. 8
0
        /// <summary>
        /// Time complexity: O(n^2).
        /// </summary>
        public static T[] Sort(T[] array, SortDirection sortDirection = SortDirection.Ascending)
        {
            var comparer = new CustomComparer <T>(sortDirection, Comparer <T> .Default);
            var swapped  = true;

            while (swapped)
            {
                swapped = false;

                for (int i = 0; i < array.Length - 1; i++)
                {
                    //compare adjacent elements
                    if (comparer.Compare(array[i], array[i + 1]) > 0)
                    {
                        var temp = array[i];
                        array[i]     = array[i + 1];
                        array[i + 1] = temp;
                        swapped      = true;
                    }
                }
            }

            return(array);
        }