예제 #1
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);
            }
        }
예제 #2
0
 public Drawer(SortingArray arrayToDraw, Graphics graph, RectangleF drawingArea)
 {
     NextFrameDrawings = new Dictionary <int, DrawingOptions>();
     this.arrayToDraw  = arrayToDraw;
     this.graph        = graph;
     this.drawingArea  = drawingArea;
 }
예제 #3
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);
        }
예제 #4
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--;
            }
        }
예제 #5
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);
                }
            }
        }
예제 #6
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);
                }
            }
        }
예제 #7
0
 static void BitonicCompare(SortingArray array, int source, int destination, bool direction)
 {
     if (direction == array.IsBigger(source, destination))
     {
         array.Swap(source, destination);
     }
     Updated(array);
 }
예제 #8
0
 public static void GenerateReversed(SortingArray array)
 {
     for (int i = 0; i < array.Length; i++)
     {
         array.Modify(i, array.Length - i);
         Updated(array);
     }
 }
예제 #9
0
 public static void GenerateRandomAscending(SortingArray array)
 {
     for (int i = 0; i < array.Length; i++)
     {
         StandardGenerator rand = new StandardGenerator();
         int value = rand.Next(i, array.Length) + 1;
         array.Modify(i, value);
         Updated(array);
     }
 }
예제 #10
0
 public static void GenerateRandom(SortingArray array)
 {
     for (int i = 0; i < array.Length; i++)
     {
         StandardGenerator rand = new StandardGenerator();
         int val = rand.Next(1, array.Length + 1);
         array.Modify(i, val);
         Updated(array);
     }
 }
예제 #11
0
 public static void Shuffle(SortingArray array)
 {
     for (int i = 0; i < array.Length; i++)
     {
         StandardGenerator rand = new StandardGenerator();
         int pos = rand.Next(0, array.Length);
         array.Swap(i, pos);
         Updated(array);
     }
 }
예제 #12
0
        public void StartAsync(SortingArray array, Algorithm algorithm)
        {
            if (!IsWorking)
            {
                currentAlgorithm = algorithm;
                currentArray     = array;

                workThread = new Thread(DoWork);
                workThread.Start();
            }
        }
예제 #13
0
        static void BitonicSortRecursion(SortingArray array, int index, int length, bool direction)
        {
            if (length > 1)
            {
                int median = (length / 2);

                BitonicSortRecursion(array, index, median, true);
                BitonicSortRecursion(array, index + median, median, false);
                BitonicMerge(array, index, length, direction);
            }
        }
예제 #14
0
 public static void MaxHeapSort(SortingArray array)
 {
     for (int i = array.Length / 2 - 1; i >= 0; i--)
     {
         MaxHeapify(array, array.Length, i);
     }
     for (int i = array.Length - 1; i >= 0; i--)
     {
         array.Swap(0, i);
         Updated(array);
         MaxHeapify(array, i, 0);
     }
 }
예제 #15
0
        static void BitonicMerge(SortingArray array, int index, int length, bool direction)
        {
            if (length > 1)
            {
                int median = (length / 2);

                for (int i = index; i < index + median; i++)
                {
                    BitonicCompare(array, i, i + median, direction);
                }
                BitonicMerge(array, index, median, direction);
                BitonicMerge(array, index + median, median, direction);
            }
        }
예제 #16
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);
         }
     }
 }
예제 #17
0
        public static void GenerateCubic(SortingArray array)
        {
            double a = array.Length / 2;
            double n = 1 / (a * a);

            for (int x = 0; x < array.Length; x++)
            {
                double t = x - a;
                int    y = Math.Min(array.Length, (int)(n * (t * t * t) + a) + 1);
                array.Modify(x, y);
                Updated(array);
            }
            Shuffle(array);
        }
예제 #18
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;
            }
        }
예제 #19
0
 public static void GenerateSortedAndShuffleNPercent(SortingArray array, int chance)
 {
     GenerateSorted(array);
     for (int i = 0; i < array.Length; i++)
     {
         StandardGenerator rand = new StandardGenerator();
         int randNum            = rand.Next(101);
         if (randNum <= chance)
         {
             int pos = rand.Next(array.Length);
             array.Swap(i, pos);
             Updated(array);
         }
     }
 }
예제 #20
0
        public static void Generate8Blocked(SortingArray array)
        {
            double d   = array.Length / 8.0;
            double val = d;

            for (int i = 0; i < array.Length; i++)
            {
                if (i >= val)
                {
                    val += d;
                }
                array.Modify(i, (int)val);
                Updated(array);
            }
            Shuffle(array);
        }
예제 #21
0
 public static void SelectionSort(SortingArray array)
 {
     for (int i = array.Length - 1; i > 0; i--)
     {
         int biggest = 0;
         for (int j = 0; j <= i; j++)
         {
             if (array.IsBiggerOrEqual(j, biggest))
             {
                 biggest = j;
             }
             Updated(array);
         }
         array.Swap(biggest, i);
         Updated(array);
     }
 }
예제 #22
0
        public static Color GetColor(SortingArray array, int pos, DrawingColor color)
        {
            switch (color)
            {
            case DrawingColor.Default:
                return(GetDefaultColor(array, pos));

            case DrawingColor.Selected:
                return(GetSelectedColor(array, pos));

            case DrawingColor.Swapped:
                return(GetSwappedColor(array, pos));

            case DrawingColor.Special:
                return(GetSpecialColor(array, pos));
            }
            return(Color.WhiteSmoke);
        }
예제 #23
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);
         }
     }
 }
예제 #24
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);
        }
예제 #25
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;
         }
     }
 }
예제 #26
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);
                }
            }
        }
예제 #27
0
        static Color GetSpecialColor(SortingArray array, int pos)
        {
            switch (style)
            {
            case DrawingStyle.Default:
                return(DefaultSpecialColor);

            case DrawingStyle.Rainbow:
                return(RainbowSpecialColor(array.Get(pos), array.Length - 1));

            case DrawingStyle.DistanceBased:
                return(DistanceSpecialColor());

            case DrawingStyle.Monochrome:
                return(Color.White);

            case DrawingStyle.Custom:
                return(CustomSpecialColor);

            default:
                return(Color.Gray);
            }
        }
예제 #28
0
        static Color GetDefaultColor(SortingArray array, int pos)
        {
            switch (style)
            {
            case DrawingStyle.Default:
                return(DefaultDefaultColor);

            case DrawingStyle.Rainbow:
                return(RainbowDefaultColor(array.Get(pos), array.Length - 1));

            case DrawingStyle.DistanceBased:
                return(DistanceDefaultColor(array.DistanceToSortedPosition(pos), array.Length));

            case DrawingStyle.Monochrome:
                return(Color.LightSlateGray);

            case DrawingStyle.Custom:
                return(CustomDefaultColor);

            default:
                return(Color.WhiteSmoke);
            }
        }
예제 #29
0
        static Color GetSelectedColor(SortingArray array, int pos)
        {
            switch (style)
            {
            case DrawingStyle.Default:
                return(DefaultSelectedColor);

            case DrawingStyle.Rainbow:
                return(RainbowSelectedColor(array.Get(pos), array.Length - 1));

            case DrawingStyle.DistanceBased:
                return(DistanceSelectedColor(array.DistanceToSortedPosition(pos), array.Length));

            case DrawingStyle.Monochrome:
                return(Color.LightGray);

            case DrawingStyle.Custom:
                return(CustomSelectedColor);

            default:
                return(Color.LightGray);
            }
        }
예제 #30
0
        static Color GetSwappedColor(SortingArray array, int pos)
        {
            switch (style)
            {
            case DrawingStyle.Default:
                return(DefaultSwappedColor);

            case DrawingStyle.Rainbow:
                return(RainbowSwappedColor());

            case DrawingStyle.DistanceBased:
                return(DistanceSwappedColor());

            case DrawingStyle.Monochrome:
                return(Color.White);

            case DrawingStyle.Custom:
                return(CustomSwappedColor);

            default:
                return(Color.LightGray);
            }
        }