如何可视化算法的步骤

时间:2017-02-14 14:43:41

标签: c# wpf graphics

如何延迟算法的执行以直观地显示算法中每次迭代的结果?当我尝试更新下面代码中对象的高度时,它只显示最终结果。如何显示每一步发生的事情?

namespace BubbleSortRappresentazione_grafica
{     
    public partial class MainWindow : Window
    {
        int[] v;

        public MainWindow()
        {
            InitializeComponent();

            Random rnd = new Random();

            v = new int[10];
            for (int j = 0; j < 10; j++)
            {
                v[j] = rnd.Next(0, 100);   
            }
            _0.Height = v[0] + 20;
            _1.Height = v[1] + 20;
            _2.Height = v[2] + 20;
            _3.Height = v[3] + 20;
            _4.Height = v[4] + 20;
            _5.Height = v[5] + 20;
            _6.Height = v[6] + 20;
            _7.Height = v[7] + 20;
            _8.Height = v[8] + 20;
            _9.Height = v[9] + 20;   
        }    

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            // algoritmo ordinamento    

            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {                       
                    if (v[j] > v[i])
                    {   
                        int tmp = v[j];
                        v[j] = v[i];
                        v[i] = tmp;

                        _0.Height = v[0] + 20;
                        _0.Content = v[0];
                        _1.Height = v[1] + 20;
                        _1.Content = v[1] ;
                        _2.Height = v[2] + 20;
                        _2.Content = v[2];
                        _3.Height = v[3] + 20;
                        _3.Content = v[3] ;
                        _4.Height = v[4] + 20;
                        _4.Content = v[4];
                        _5.Height = v[5] + 20;
                        _5.Content = v[5];
                        _6.Height = v[6] + 20;
                        _6.Content = v[6];
                        _7.Height = v[7] + 20;
                        _7.Content = v[7];
                        _8.Height = v[8] + 20;
                        _8.Content = v[8];
                        _9.Height = v[9] + 20;
                        _9.Content = v[9];

                        Thread.Sleep(100);   
                    }   
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

我相信你真正想要的是Application.DoEvents方法:

.Sleep()

这将导致您在循环内进行的任何视觉更改都将在每次迭代中应用。你也可以考虑删除@SpringBootApplication //@ImportResource({"classpath:dubbo.xml"}) //I want to specify the xml file with command line arguments at run time public class App { public static void main(String[] args) { // How to import the XML file now? SpringApplication.run(App.class, args); } } ,因为我不确定它对你有多大用处。