Exemplo n.º 1
0
        public override StateAutomaton DoStepForward()
        {
            var isInterestingState = false;
            StepsCount++;

            StateAutomaton state = new StateHeapSortAutomaton(-1, -1, -1, (int)dataModel.State, "", dataModel.Array, -1);  
            while (!isInterestingState)
            {
                switch (dataModel.State)
                {
                    case States.InitialState:
                    {
                        dataModel.State = States.StartLoopBuildingPyramid;
                        break;
                    }
                    case States.StartLoopBuildingPyramid:
                    {
                       dataModel.Index = (dataModel.ArraySize / 2) - 1;
                       
                        dataModel.State = States.LoopBuildingPyramid;
                       
                        break;
                    }
                    case States.LoopBuildingPyramid:
                    {
                        dataModel.State = dataModel.Index >= 0 ? States.SiftingElementInBuildingPyramid : States.StartLoopSorting;

                        break;
                    }
                    case States.SiftingElementInBuildingPyramid:
                    {
                       if (automatonSiftDown.State != AutomatonSiftDown.States.FinalState)
                       {
                           isInterestingState = true;
                           state = automatonSiftDown.DoStepForward(dataModel.Index, dataModel.ArraySize, dataModel.SortedPart);

                           dataModel.State = States.SiftingElementInBuildingPyramid;
                        }
                        else
                       {
                           automatonSiftDown.State = AutomatonSiftDown.States.InitialState;
                           dataModel.State = States.DecrementCounterInBuildingPyramid;
                        }

                        break;
                    }
                    case States.DecrementCounterInBuildingPyramid:
                    {
                        dataModel.Index--;
                        dataModel.State = States.LoopBuildingPyramid;

                        break;
                    }
                    case States.StartLoopSorting:
                    {
                       dataModel.Index = dataModel.ArraySize;
                       
                       dataModel.State = States.LoopSorting;
                       
                       break;
                    }
                    case States.LoopSorting:
                    {
                       // MessageBox.Show(dataModel.Index.ToString());
                        dataModel.State = dataModel.Index > 1 ? States.SwappingElements : States.FinalState;

                        break;
                    }
                    case States.SwappingElements:
                    {
                        state = GetStateHeapSortAutomaton(dataModel.State);
                        isInterestingState = true;
                        
                        dataModel.SwappingElements();
                        
                        dataModel.SortedPart = dataModel.Index - 1;
                       
                        dataModel.State = States.SiftingElementInSorting;
                       
                        break;
                    }
                    case States.SiftingElementInSorting:
                    {
                        
                        if (automatonSiftDown.State != AutomatonSiftDown.States.FinalState && automatonSiftDown.State != AutomatonSiftDown.States.EndLoop)
                        {
                            isInterestingState = true;
                            state = automatonSiftDown.DoStepForward(0, dataModel.Index - 1, dataModel.SortedPart);

                            dataModel.State = States.SiftingElementInSorting;
                        }
                        else
                        {
                            automatonSiftDown.State = AutomatonSiftDown.States.InitialState;
                            
                            dataModel.State = States.DecrementCounterSorting;
                        }
                        
                        break;
                    }
                    case States.DecrementCounterSorting:
                    {
                       dataModel.Index--;
                       
                        dataModel.State = States.LoopSorting;
                       
                        break;
                    }
                    case States.FinalState:
                    {
                        state = GetStateHeapSortAutomaton(dataModel.State);
                        
                        isInterestingState = true;
                       
                        break;
                    }
                }

            }

            return state;
        }
Exemplo n.º 2
0
        public StateAutomaton DoStepForward(int i, int j, int sortedPart)
        {
            var isInterestingState = false;

            StateAutomaton state = new StateHeapSortAutomaton(-1, -1, -1, (int) dataModel.State, "", dataModel.Array,
                dataModel.SortedPart);

            while (!isInterestingState)
            {
                switch (dataModel.State)
                {
                    case States.InitialState:
                    {
                        dataModel.State = States.InitializeVariables;
                        break;
                    }
                    case States.InitializeVariables:
                    {
                        dataModel.IsDone = false;

                        dataModel.State = States.StartLoop;

                        break;
                    }
                    case States.StartLoop:
                    {
                        dataModel.SortedPart = sortedPart;

                        dataModel.Counter = i;

                        dataModel.State = States.Loop;

                        break;
                    }
                    case States.Loop:
                    {
                        isInterestingState = true;

                        state = GetStateShiftDownAutomaton(dataModel.State);

                        dataModel.State = dataModel.Counter*2 + 1 < j && !dataModel.IsDone
                            ? States.InitializingIndexMaximumChild
                            : States.FinalState;

                        break;
                    }
                    case States.InitializingIndexMaximumChild:
                    {
                        dataModel.IndexMaximumChild = dataModel.Counter*2 + 1;

                        dataModel.State = States.ConditionOnUpdateMaximumChild;

                        break;
                    }
                    case States.ConditionOnUpdateMaximumChild:
                    {
                        isInterestingState = true;

                        state = GetStateShiftDownAutomaton(dataModel.State);

                        dataModel.State = dataModel.Counter*2 + 2 < j &&
                                          dataModel.Array[dataModel.IndexMaximumChild] <
                                          dataModel.Array[dataModel.Counter*2 + 2]
                            ? States.UpdateMaximalChild
                            : States.EndingConditionOnUpdateMaximumChild;

                        break;
                    }
                    case States.UpdateMaximalChild:
                    {
                        dataModel.IndexMaximumChild = dataModel.Counter*2 + 2;

                        dataModel.State = States.EndingConditionOnUpdateMaximumChild;

                        break;
                    }
                    case States.EndingConditionOnUpdateMaximumChild:
                    {
                        isInterestingState = true;

                        state = GetStateShiftDownAutomaton(dataModel.State);

                        dataModel.State = States.ConditionOnUpdateParent;

                        break;
                    }
                    case States.ConditionOnUpdateParent:
                    {
                        isInterestingState = true;

                        state = GetStateShiftDownAutomaton(dataModel.State);

                        dataModel.State = dataModel.Array[dataModel.Counter] <
                                          dataModel.Array[dataModel.IndexMaximumChild]
                            ? States.SwappingParentWithMaximumChild
                            : States.EndLoop;

                        break;
                    }
                    case States.SwappingParentWithMaximumChild:
                    {
                        isInterestingState = true;

                        state = GetStateShiftDownAutomaton(dataModel.State);

                        dataModel.SwapParentWithMaximumChild();

                        dataModel.State = States.EndingConditionOnUpdateParent;

                        break;
                    }
                    case States.EndLoop:
                    {
                        state = GetStateShiftDownAutomaton(dataModel.State);

                        isInterestingState = true;

                        dataModel.IsDone = true;

                        dataModel.State = States.FinalState;

                        break;
                    }
                    case States.EndingConditionOnUpdateParent:
                    {
                        dataModel.State = States.Loop;

                        break;
                    }
                    case States.FinalState:
                    {
                        state = GetStateShiftDownAutomaton(States.EndLoop);
                        isInterestingState = true;

                        break;
                    }
                }
            }

            return state;
        }