Esempio n. 1
0
 private void CopyToAux(IComparable[] arr, Bound b)
 {
     for (var i = b.Low; i <= b.High; i++)
     {
         _aux[i] = arr[i];
     }
 }
Esempio n. 2
0
        private Bound Merge(IComparable[] a, Bound l, Bound h)
        {
            if (l.Low == l.High)
            {
                CopyToAux(a, h);
                return h;
            }
            if (h.Low == h.High)
            {
                CopyToAux(a, l);
                return l;
            }

            //if (!Less(a[l.Low], a[h.Low]))
            if (a[l.Low].CompareTo(a[h.Low]) <= 0)
            {
                _aux[l.Low] = a[l.Low];
                Merge(a, new Bound() { Low = l.Low + 1, High = l.High }, h);

                return l;
            }
            else
            {
                _aux[h.Low] = a[h.Low];
                Merge(a, l, new Bound() { Low = h.Low + 1, High = h.High });

                return h;
            }
        }
Esempio n. 3
0
        //private IComparable[] MergeSort(IComparable[] a)
        //{
        //    IComparable[] a1 = new IComparable[a.Length / 2];
        //    IComparable[] a2 = new IComparable[a.Length - a.Length / 2];
        //    Array.Copy(a, a1, a.Length / 2);
        //    Array.Copy(a, a.Length / 2, a2, 0, a.Length - a.Length / 2);
        //    if (a.Length > 1)
        //        return Merge(MergeSort(a1), MergeSort(a2));
        //    return a;
        //}
        private Bound MergeSort(IComparable[] a, Bound b)
        {
            if (b.Length > 1)
            {
                var lowBound = new Bound { Low = b.Low, High = b.Middle };
                var highBound = new Bound { Low = b.Middle + 1, High = b.High };

                return Merge(a, MergeSort(a, lowBound), MergeSort(a, highBound));
            }
            else
                return b;
        }