Example #1
0
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            int[] stacks = sortingStacks.stacks;
            int   gap    = stackCount / 2;

            while (gap >= 1)
            {
                for (int i = 0; i < gap; i++)
                {
                    for (int j = i; j < stackCount; j += gap)
                    {
                        int smallest = int.MaxValue;
                        for (int k = j; k < stackCount; k += gap)
                        {
                            if (stacks[k] < smallest)
                            {
                                smallest = stacks[k];
                            }
                        }
                        if (stacks[j] != smallest)
                        {
                            sortingStacks.SwapStacks(smallest, stacks[j]);
                            yield return(new WaitForSeconds(sortingStacks.delay));
                        }
                    }
                }
                gap /= 2;
            }
            yield return(null);

            sortingStacks.StopSort();
        }
Example #2
0
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            int[] stacks          = sortingStacks.stacks;
            int   comparisonDigit = 1;

            while (comparisonDigit < stackCount)
            {
                int digit;
                for (int i = 0; i < stackCount; i++)
                {
                    int smallestDigit = 10;
                    int index         = i;
                    for (int j = i; j < stackCount; j++)
                    {
                        digit = stacks[j] < comparisonDigit ? 0 : (stacks[j] / comparisonDigit) % 10;
                        if (digit < smallestDigit)
                        {
                            smallestDigit = digit;
                            index         = j;
                        }
                    }
                    if (index != i)
                    {
                        sortingStacks.SetStack(stacks[index], i);
                        yield return(new WaitForSeconds(sortingStacks.delay));
                    }
                }
                comparisonDigit *= 10;
            }
            sortingStacks.StopSort();
        }
Example #3
0
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            int[] stacks     = sortingStacks.stacks;
            int   groupCount = 0;
            int   groupSize  = 2;

            for (int i = 0; i < stackCount; i += groupSize)
            {
                groupCount++;
                if (i + (groupSize / 2) >= stackCount)
                {
                    break;
                }
                if (stacks[i] > stacks[i + 1])
                {
                    sortingStacks.SwapStackIndices(i, i + 1);
                    yield return(new WaitForSeconds(sortingStacks.delay));
                }
            }
            groupSize *= 2;
            while (groupCount > 1)
            {
                for (int i = 0; i < stackCount; i += groupSize)
                {
                    if (i + (groupSize / 2) >= stackCount)
                    {
                        break;
                    }
                    for (int j = i; j < i + groupSize; j++)
                    {
                        if (j >= stackCount)
                        {
                            break;
                        }
                        int smallest = int.MaxValue;
                        for (int k = j; k < i + groupSize; k++)
                        {
                            if (stacks[k] < smallest)
                            {
                                smallest = stacks[k];
                            }
                        }
                        if (stacks[j] != smallest)
                        {
                            sortingStacks.SetStack(smallest, j);
                            yield return(new WaitForSeconds(sortingStacks.delay));
                        }
                    }
                    groupCount--;
                }
                groupSize *= 2;
            }
            sortingStacks.StopSort();
        }
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            while (!sortingStacks.StacksSorted())
            {
                for (int i = 1; i < stackCount; i++)
                {
                    if (sortingStacks.stacks[i] < sortingStacks.stacks[i - 1])
                    {
                        yield return(new WaitForSeconds(sortingStacks.delay));

                        sortingStacks.SetStack(sortingStacks.stacks[i], i - 1);
                    }
                }
            }
            sortingStacks.StopSort();
        }
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            for (int i = 0; i < stackCount; i++)
            {
                yield return(new WaitForSeconds(sortingStacks.delay));

                int smallest = int.MaxValue;
                for (int j = i; j < stackCount; j++)
                {
                    if (sortingStacks.stacks[j] < smallest)
                    {
                        smallest = sortingStacks.stacks[j];
                    }
                }
                sortingStacks.SetStack(smallest, i);
            }
            sortingStacks.StopSort();
        }
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            for (int i = 0; i < stackCount; i++)
            {
                yield return(new WaitForSeconds(sortingStacks.delay));

                int index = i;
                for (int j = i; j > 0; j--)
                {
                    if (sortingStacks.stacks[j - 1] < sortingStacks.stacks[i])
                    {
                        break;
                    }
                    index--;
                }
                sortingStacks.SetStack(sortingStacks.stacks[i], index);
            }
            sortingStacks.StopSort();
        }
Example #7
0
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            int[] stacks = sortingStacks.stacks;
            for (int i = (stackCount / 2) - 1; i >= 0; i--)
            {
                bool sorting = true;
                int  index   = i;
                while (sorting)
                {
                    int largest = index;
                    int left    = (2 * index) + 1;
                    int right   = (2 * index) + 2;
                    if (left < stackCount && stacks[left] > stacks[largest])
                    {
                        largest = left;
                    }
                    if (right < stackCount && stacks[right] > stacks[largest])
                    {
                        largest = right;
                    }
                    if (largest != index)
                    {
                        sortingStacks.SwapStackIndices(index, largest);
                        yield return(new WaitForSeconds(sortingStacks.delay));

                        index = largest;
                    }
                    else
                    {
                        sorting = false;
                    }
                }
            }
            for (int i = stackCount - 1; i > 0; i--)
            {
                sortingStacks.SwapStackIndices(0, i);
                yield return(new WaitForSeconds(sortingStacks.delay));

                bool sorting = true;
                int  index   = 0;
                while (sorting)
                {
                    int largest = index;
                    int left    = (2 * index) + 1;
                    int right   = (2 * index) + 2;
                    if (left < i && stacks[left] > stacks[largest])
                    {
                        largest = left;
                    }
                    if (right < i && stacks[right] > stacks[largest])
                    {
                        largest = right;
                    }
                    if (largest != index)
                    {
                        sortingStacks.SwapStackIndices(index, largest);
                        yield return(new WaitForSeconds(sortingStacks.delay));

                        index = largest;
                    }
                    else
                    {
                        sorting = false;
                    }
                }
            }
            sortingStacks.StopSort();
        }
Example #8
0
        public static IEnumerator Sort(SortingStacks _sortingStacks)
        {
            sortingStacks = _sortingStacks;
            int stackCount = sortingStacks.stackCount;

            int[] stacks        = sortingStacks.stacks;
            int   partitionSize = stackCount;

            while (!sortingStacks.StacksSorted() && partitionSize > 1)
            {
                for (int startIndex = 0; startIndex < stackCount; startIndex += partitionSize)
                {
                    int endIndex = startIndex + partitionSize;
                    if (endIndex > stackCount)
                    {
                        continue;
                    }
                    if (endIndex + partitionSize > stackCount)
                    {
                        endIndex = stackCount;
                    }
                    int midIndex = (startIndex + endIndex) / 2;
                    int pivot    = GetPivotStack(stacks, startIndex, endIndex);
                    sortingStacks.SetStack(pivot, midIndex);
                    for (int i = startIndex; i < midIndex; i++)
                    {
                        if (stacks[i] > pivot)
                        {
                            for (int j = midIndex + 1; j < endIndex; j++)
                            {
                                if (pivot < stacks[j])
                                {
                                    continue;
                                }
                                sortingStacks.SwapStackIndices(i, j);
                                yield return(new WaitForSeconds(sortingStacks.delay));

                                break;
                            }
                        }
                    }
                }
                partitionSize /= 2;
            }
            while (!sortingStacks.StacksSorted())
            {
                for (int i = 0; i < stackCount; i++)
                {
                    if (i > 0 && stacks[i - 1] > stacks[i])
                    {
                        int j = i - 1;
                        while (j > 0 && stacks[j] > stacks[i])
                        {
                            j--;
                        }
                        sortingStacks.SetStack(stacks[i], j + 1);
                        yield return(new WaitForSeconds(sortingStacks.delay));
                    }
                }
            }
            sortingStacks.StopSort();
        }