示例#1
0
        public static void OddEvenSort(SortingArray array)
        {
            var sorted = false;

            while (!sorted)
            {
                sorted = true;
                for (var i = 1; i < array.Length - 1; i += 2)
                {
                    if (array.IsBigger(i, i + 1))
                    {
                        array.Swap(i, i + 1);
                        sorted = false;
                    }
                    Updated(array);
                }

                for (var i = 0; i < array.Length - 1; i += 2)
                {
                    if (array.IsBigger(i, i + 1))
                    {
                        array.Swap(i, i + 1);
                        sorted = false;
                    }
                    Updated(array);
                }
            }
        }
示例#2
0
        static void MaxHeapify(SortingArray array, int n, int i)
        {
            int largest = i;
            int l       = 2 * i + 1;
            int r       = 2 * i + 2;

            if (l < n)
            {
                if (array.IsBigger(l, largest))
                {
                    largest = l;
                }

                Updated(array);
            }

            if (r < n)
            {
                if (array.IsBigger(r, largest))
                {
                    largest = r;
                }

                Updated(array);
            }

            if (largest != i)
            {
                array.Swap(i, largest);
                Updated(array);
                MaxHeapify(array, n, largest);
            }
        }
示例#3
0
        public static void CocktailSort(SortingArray array)
        {
            int left  = 0;
            int right = array.Length - 1;

            while (left < right)
            {
                for (int i = left; i < right; i++)
                {
                    if (array.IsBigger(i, i + 1))
                    {
                        array.Swap(i, i + 1);
                    }
                    Updated(array);
                }

                for (int i = right; i > left; i--)
                {
                    if (array.IsSmaller(i, i - 1))
                    {
                        array.Swap(i, i - 1);
                    }
                    Updated(array);
                }

                left++;
                right--;
            }
        }
示例#4
0
        static void QuickSortRecursion(SortingArray array, int left, int right)
        {
            if (left < right)
            {
                int pivot = right;

                int l = left;
                int r = right;

                while (l < r)
                {
                    if (array.IsBigger(l, pivot))
                    {
                        while (l < r && array.IsBiggerOrEqual(r, pivot))
                        {
                            r--;
                            Updated(array);
                        }
                        array.Swap(l, r);
                    }
                    l++;
                    Updated(array);
                }
                if (l > left)
                {
                    QuickSortRecursion(array, left, l - 1);
                }
                if (r < right)
                {
                    array.Swap(r, pivot);
                    QuickSortRecursion(array, r, right);
                }
            }
        }
示例#5
0
        public static void ShellSort(SortingArray array)
        {
            int distance = array.Length / 2;

            while (distance > 1)
            {
                for (int i = distance; i < array.Length; i++)
                {
                    int j = i;
                    while (j >= 0)
                    {
                        Updated(array);
                        j -= distance;
                        if (array.IsBigger(j, j + distance))
                        {
                            array.Swap(j, j + distance);
                        }
                        else
                        {
                            break;
                        }
                    }
                }

                distance = (distance + 1) / 2;
            }

            InsertionSort(array);
        }
示例#6
0
 static void BitonicCompare(SortingArray array, int source, int destination, bool direction)
 {
     if (direction == array.IsBigger(source, destination))
     {
         array.Swap(source, destination);
     }
     Updated(array);
 }
示例#7
0
 public static void BubbleSort(SortingArray array)
 {
     for (int i = array.Length - 1; i >= 0; i--)
     {
         for (int j = 0; j < i; j++)
         {
             if (array.IsBigger(j, j + 1))
             {
                 array.Swap(j, j + 1);
             }
             Updated(array);
         }
     }
 }
示例#8
0
        public static void OptimizedCocktailSort(SortingArray array)
        {
            int left  = 0;
            int right = array.Length - 1;

            while (left < right)
            {
                int nextLeft  = right;
                int nextRight = left;

                bool sorted = true;

                for (int i = left; i < right; i++)
                {
                    if (array.IsBigger(i, i + 1))
                    {
                        array.Swap(i, i + 1);
                        sorted    = false;
                        nextRight = i;
                    }
                    Updated(array);
                }

                if (sorted)
                {
                    break;
                }

                right = nextRight;

                for (int i = right; i > left; i--)
                {
                    if (array.IsSmaller(i, i - 1))
                    {
                        array.Swap(i, i - 1);
                        sorted   = false;
                        nextLeft = i;
                    }
                    Updated(array);
                }

                if (sorted)
                {
                    break;
                }

                left = nextLeft;
            }
        }
示例#9
0
 public static void InsertionSort(SortingArray array)
 {
     for (int i = 1; i < array.Length; i++)
     {
         for (int j = i; j > 0; j--)
         {
             if (array.IsBigger(j - 1, j))
             {
                 array.Swap(j - 1, j);
             }
             else
             {
                 Updated(array);
                 break;
             }
             Updated(array);
         }
     }
 }
示例#10
0
        public static void CombSort(SortingArray array)
        {
            int distance = (int)(array.Length / 1.3);

            while (distance > 1)
            {
                for (int i = 0; i < array.Length - distance; i++)
                {
                    if (array.IsBigger(i, i + distance))
                    {
                        array.Swap(i, i + distance);
                    }
                    Updated(array);
                }
                distance = (int)(distance / 1.3);
            }

            OptimizedBubbleSort(array);
        }
示例#11
0
 public static void OptimizedBubbleSort(SortingArray array)
 {
     for (int i = array.Length - 1; i >= 0; i--)
     {
         bool sorted = true;
         for (int j = 0; j < i; j++)
         {
             if (array.IsBigger(j, j + 1))
             {
                 array.Swap(j, j + 1);
                 sorted = false;
             }
             Updated(array);
         }
         if (sorted)
         {
             break;
         }
     }
 }
示例#12
0
        static void MergeSortRecursion(SortingArray array, int[] temp, int left, int right)
        {
            if (left < right)
            {
                int len = right - left + 1;
                int mid = left + len / 2;
                MergeSortRecursion(array, temp, left, mid - 1);
                MergeSortRecursion(array, temp, mid, right);

                int p1 = left;
                int p2 = mid;

                for (int i = 0; i < len; i++)
                {
                    bool first = true;

                    if ((p1 >= mid) | (p2 <= right && array.IsBigger(p1, p2)))
                    {
                        first = false;
                    }

                    if (first)
                    {
                        temp[i] = array.Get(p1);
                        p1++;
                    }
                    else
                    {
                        temp[i] = array.Get(p2);
                        p2++;
                    }
                    Updated(array);
                }

                for (int i = 0; i < len; i++)
                {
                    array.Modify(left + i, temp[i]);
                    Updated(array);
                }
            }
        }