예제 #1
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();
        }
예제 #2
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();
        }
예제 #3
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();
        }