public void ArraySortSelection_Void(int[] ArrayIn, int[] ArrayOut, bool real,
                                            MyArraySortDirection direction = MyArraySortDirection.LowToHight)
        {
            MyArray <int> .ArraySortSelection(ArrayIn, direction);

            Assert.AreEqual(real, MyArray <int> .ArrayCompareInt(ArrayIn, ArrayOut));
        }
Example #2
0
        public static void ArraySortShells(T[] arrayIn, MyArraySortDirection direction)
        {
            int i = 0;
            int j = 0;
            T   temp;
            int step = 0;

            for (step = arrayIn.Length - 1; step > 0; step--)
            {
                i = 0;
                for (i = 0; i < arrayIn.Length - 1; i++)
                {
                    j = i + step;
                    if (j > arrayIn.Length - 1)
                    {
                        j = arrayIn.Length - 1;
                    }
                    if (((arrayIn[i].CompareTo(arrayIn[j]) > 0) && (direction == MyArraySortDirection.LowToHight))
                        ||
                        ((arrayIn[i].CompareTo(arrayIn[j]) < 0) && (direction == MyArraySortDirection.HightToLow)))
                    {
                        temp       = arrayIn[i];
                        arrayIn[i] = arrayIn[j];
                        arrayIn[j] = temp;
                    }
                }
            }
            return;
        }
Example #3
0
        public static void ArraySort(T[] arrayIn, MyArraySortDirection direction,
                                     ArraySortMethod sort = ArraySortMethod.QuickSort)
        {
            //SortSelection, SortInsertion, SortBuble,SortShells, QuickSort
            switch (sort)
            {
            case ArraySortMethod.QuickSort:
                ArrayQuickSort(arrayIn, direction);
                break;

            case ArraySortMethod.SortSelection:
                ArraySortSelection(arrayIn, direction);
                break;

            case ArraySortMethod.SortInsertion:
                ArraySortInsertion(arrayIn, direction);
                break;

            case ArraySortMethod.SortBuble:
                ArraySortBuble(arrayIn, direction);
                break;

            case ArraySortMethod.SortShells:
                ArraySortShells(arrayIn, direction);
                break;

            default:
                throw new NotSupportedException();
                //break;
            }
        }
Example #4
0
        public static void ArraySortBuble(T[] arrayIn, MyArraySortDirection direction)
        {
            bool sorted = true;
            T    temp;
            int  leng = 0;

            while (sorted)
            {
                sorted = false;
                for (int i = 0; i < arrayIn.Length - 1 - leng; i++)
                {
                    temp = arrayIn[i];
                    if (((temp.CompareTo(arrayIn[i + 1]) > 0) && (direction == MyArraySortDirection.LowToHight))
                        ||
                        ((temp.CompareTo(arrayIn[i + 1]) < 0) && (direction == MyArraySortDirection.HightToLow)))
                    {
                        arrayIn[i]     = arrayIn[i + 1];
                        arrayIn[i + 1] = temp;
                        sorted         = true;
                    }
                }
                leng++;
            }
            return;
        }
Example #5
0
        public static void ArraySortInsertion(T[] arrayIn, MyArraySortDirection direction)
        {
            int i = 0;
            int j = 0;
            T   temp;

            for (i = 1; i < arrayIn.Length; i++)
            {
                temp = arrayIn[i];
                for (j = i - 1; j >= 0; j--)
                {
                    if (((temp.CompareTo(arrayIn[j]) > 0) && (direction == MyArraySortDirection.LowToHight))
                        ||
                        ((temp.CompareTo(arrayIn[j]) < 0) && (direction == MyArraySortDirection.HightToLow)))

                    {
                        arrayIn[j + 1] = temp;
                        break;
                    }
                    else
                    {
                        arrayIn[j + 1] = arrayIn[j];
                        if (j == 0)
                        {
                            arrayIn[j] = temp;
                        }
                    }
                }
            }
            return;
        }
        public void ArraySortMethod_Void(int[] arrayIn, int[] ArrayOut, bool real,
                                         MyArraySortDirection direction,
                                         ArraySortMethod sortmethod = ArraySortMethod.QuickSort)
        {
            MyArray <int> .ArraySort(arrayIn, direction, sortmethod);

            Assert.AreEqual(real, MyArray <int> .ArrayCompareInt(arrayIn, ArrayOut));
        }
Example #7
0
        private void TestArraySortSelectionOne(int[] ArrayIn, MyArraySortDirection direction)
        {
            MyArray <int> .ArraySortSelection(ArrayIn, direction);

            for (int i = 0; i < ArrayIn.Length; i++)
            {
                MessageBox.Show(ArrayIn[i].ToString());
            }
        }
Example #8
0
        public static void ArraySortSelection(T[] arrayIn, MyArraySortDirection direction)
        {
            int i = 0;
            int j = 0;
            T   temp;

            for (i = 0; i < arrayIn.Length; i++)
            {
                for (j = i + 1; j < arrayIn.Length; j++)
                {
                    if (
                        (direction == MyArraySortDirection.LowToHight) && (arrayIn[i].CompareTo(arrayIn[j]) > 0)
                        ||
                        (direction == MyArraySortDirection.HightToLow) && (arrayIn[i].CompareTo(arrayIn[j]) < 0)
                        )
                    {
                        temp       = arrayIn[i];
                        arrayIn[i] = arrayIn[j];
                        arrayIn[j] = temp;
                    }
                }
            }
            return;
        }
Example #9
0
        private static void Quicksort(T[] elements, int left, int right, MyArraySortDirection direction)
        {
            if (elements.Length == 0)
            {
                return;
            }

            int i = left, j = right;
            T   pivot = elements[(left + right) / 2];

            switch (direction)
            {
            case MyArraySortDirection.HightToLow:

                while (i <= j)
                {
                    while (elements[i].CompareTo(pivot) > 0)

                    {
                        i++;
                    }
                    while (elements[j].CompareTo(pivot) < 0)
                    {
                        j--;
                    }
                    if (i <= j)
                    {
                        // Swap
                        T tmp = elements[i];
                        elements[i] = elements[j];
                        elements[j] = tmp;
                        i++;
                        j--;
                    }
                }
                break;

            default:

                while (i <= j)
                {
                    while (elements[i].CompareTo(pivot) < 0)
                    {
                        i++;
                    }
                    while (elements[j].CompareTo(pivot) > 0)
                    {
                        j--;
                    }
                    if (i <= j)
                    {
                        // Swap
                        T tmp = elements[i];
                        elements[i] = elements[j];
                        elements[j] = tmp;
                        i++;
                        j--;
                    }
                }
                break;
            }
            // Recursive calls
            if (left < j)
            {
                Quicksort(elements, left, j, direction);
            }
            if (i < right)
            {
                Quicksort(elements, i, right, direction);
            }
        }
Example #10
0
 public static void ArrayQuickSort(T[] arrayIn, MyArraySortDirection direction)
 {
     Quicksort(arrayIn, 0, arrayIn.Length - 1, direction);
 }