예제 #1
0
        private DoubleLinkedList <T> Merge(DoubleLinkedList <T> leftList, DoubleLinkedList <T> rightList, int coef)
        {
            DoubleLinkedList <T> result = new DoubleLinkedList <T>();

            while (leftList.Length != 0 || rightList.Length != 0)
            {
                if (leftList.Length != 0 && rightList.Length != 0)
                {
                    if (leftList[0].CompareTo(rightList[0]) == coef || leftList[0].CompareTo(rightList[0]) == 0)
                    {
                        result.Add(leftList[0]);
                        leftList.RemoveFirst();
                    }
                    else
                    {
                        result.Add(rightList[0]);
                        rightList.RemoveFirst();
                    }
                }
                else if (leftList.Length != 0)
                {
                    result.Add(leftList[0]);
                    leftList.RemoveFirst();
                }
                else if (rightList.Length != 0)
                {
                    result.Add(rightList[0]);
                    rightList.RemoveFirst();
                }
            }

            return(result);
        }
예제 #2
0
        public void Sort()
        {
            if (Length <= 1)
            {
                return;
            }

            DoubleLinkedList leftl  = new DoubleLinkedList();
            DoubleLinkedList rightl = new DoubleLinkedList();
            int mid = Length / 2;

            for (int i = 0; i < Length; i++)
            {
                if (i < mid)
                {
                    leftl.Add(this[i]);
                }
                else
                {
                    rightl.Add(this[i]);
                }
            }
            leftl.Sort();
            rightl.Sort();
            Merge(leftl, rightl);
        }
예제 #3
0
        private DoubleLinkedList <T> MergeSort(DoubleLinkedList <T> list, int coef)
        {
            if (list.Length <= 1)
            {
                return(list);
            }

            DoubleLinkedList <T> leftList  = new DoubleLinkedList <T>();
            DoubleLinkedList <T> rightList = new DoubleLinkedList <T>();
            int mid = list.Length / 2;

            for (int i = 0; i < list.Length; i++)
            {
                if (i < mid)
                {
                    leftList.Add(list[i]);
                }
                else
                {
                    rightList.Add(list[i]);
                }
            }

            leftList  = MergeSort(leftList, coef);
            rightList = MergeSort(rightList, coef);

            return(Merge(leftList, rightList, coef));
        }
예제 #4
0
        private DoubleLinkedList CopyList(DoubleLinkedList list)
        {
            DoubleLinkedList copyList = new DoubleLinkedList();

            for (int i = 0; i < list.Length; i++)
            {
                copyList.Add(list[i]);
            }
            return(copyList);
        }
예제 #5
0
        public void SortDescending()
        {
            int[] firstElement = new int[2];
            firstElement = SearchIndexAndValueMaximumElement();
            RemoveByIndex(firstElement[0]);
            DoubleLinkedList tmpList = new DoubleLinkedList(firstElement[1]);

            while (Length != 0)
            {
                int[] maximum = SearchIndexAndValueMaximumElement();
                RemoveByIndex(maximum[0]);
                tmpList.Add(maximum[1]);
            }
            this._root = tmpList._root;
            Length     = tmpList.Length;
        }