Exemple #1
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--;
            }
        }
Exemple #2
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);
                }
            }
        }
Exemple #3
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);
                }
            }
        }
Exemple #4
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;
            }
        }
Exemple #5
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);
            }
        }
Exemple #6
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);
        }
Exemple #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);
 }
Exemple #8
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);
     }
 }
Exemple #9
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);
     }
 }
Exemple #10
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);
         }
     }
 }
Exemple #11
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);
         }
     }
 }
Exemple #12
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);
     }
 }
Exemple #13
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);
         }
     }
 }
Exemple #14
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);
        }
Exemple #15
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;
         }
     }
 }