简体   繁体   English

通过 java swing 实现快速排序可视化

[英]QuickSort visualisation via java swing

I wanted to make some kind of sorting algorithms visualisation is java swing but I am stuck on quicksort since I need to stop the partition loop every iteration so the array can redraw.我想让某种排序算法可视化是 java swing 但我坚持使用快速排序,因为我需要在每次迭代时停止分区循环,以便数组可以重绘。 That's how I wanted to do it but without any success这就是我想做的,但没有任何成功

 public int partition(int lowIndex, int highIndex,int i)
    {
        int pivot = highIndex;

        for(int j=lowIndex; j<highIndex; j++)
        {
            if(isBigger(j,pivot))
            {
                i++;
                swap(i,j);
                return i;
            }
        }
         swap(i+1,pivot);

        return i+1;
    }

Didn't find any good solution to keep track on i as well.也没有找到任何好的解决方案来跟踪我。 I'm just clueless我只是一无所知

Google for "Java swing visualize sorting algorithms" and you'll find many hits.谷歌搜索“Java swing 可视化排序算法”,你会发现很多命中。

For example:例如:

Code Review: sorting algorithm visualization program Code Review:排序算法可视化程序

Key points:关键点:

  • You need to modify your "sort" code to trigger some kind of "event" at each step (eg every time you swap elements):您需要修改“排序”代码以在每一步触发某种“事件”(例如,每次交换元素时):

    EXAMPLE:例子:

     public class BubbleSort implements SortingAlgorithm {... @Override public void doSort(int[] nums) {... SortingAlgorithm.setCurrentBar(j+1); SortingAlgorithm.sleepFor(delay);
  • The "event handler" will redraw the array (or, more accurately, request the Event Dispatcher thread (EDT) to repaint). “事件处理程序”将重绘数组(或者更准确地说,请求事件调度程序线程 (EDT) 重绘)。

  • Consequently, the event handler needs to "know about" the array, and the current index因此,事件处理程序需要“了解”数组和当前索引

    EXAMPLE:例子:

     public abstract interface SortingAlgorithm {... public abstract void doSort(int[] nums); public abstract void changeDelay(int delay); public static void setCurrentBar(int currentBarIndex) { PaintSurface.currentBarIndex = currentBarIndex; }...
  • There also needs to be some kind of "delay" between each step每一步之间也需要有某种“延迟”

  • This example uses SwingUtilities.invokeLater() .此示例使用SwingUtilities.invokeLater() The example camickr suggests SwingWorker.示例camickr建议使用 SwingWorker。

I hope that gives you a few ideas, and points you in the right direction!我希望这能给您一些想法,并为您指明正确的方向!

Instead of calling partition from other class I implemented partition() and sort() methods in an anonymous SwingWorker class and after every swap in partition() method I called publish(array).我没有从其他 class 调用分区,而是在匿名 SwingWorker class 中实现了 partition() 和 sort() 方法,并且在每次交换 partition() 方法之后我调用了 publish(array)。 Uploading source code if anyone would like to see how I solved this problem or would need help himself.如果有人想看看我是如何解决这个问题的或者自己需要帮助,请上传源代码。 Any feedback is really appreciate since it's my first "bigger" project任何反馈都非常感谢,因为这是我的第一个“更大”项目

 private  void startThread()
    {
            SwingWorker sw1 = new SwingWorker()  {
                public int partition(int lowIndex, int highIndex) {
                    int pivot = highIndex;
                    int i = lowIndex - 1;
                    for (int j = lowIndex; j < highIndex; j++) {
                        if (sorter.isBigger(pivot, j)) {

                            i++;
                            sorter.swap(i, j);
                            try {
                                Thread.sleep(100);
                            }
                            catch(Exception e)
                            {
                                // not implemented yet
                            }
                            publish(sorter.getArray());
                        }

                    }
                    sorter.swap(i+1,pivot);
                    publish(sorter.getArray());
                    return i+1;
                }
                public void sort(int lowIndex, int highIndex)
                {

                    if(lowIndex < highIndex)
                    {
                        int i = partition(lowIndex,highIndex);
                        try{
                           sort(lowIndex,i-1) ;
                        }
                        finally {
                            sort(i+1, highIndex);
                        }

                    }
                }
                @Override
                protected int[] doInBackground() throws Exception {
                    sorter.setArray(drafter.getArray());
                    while (!sorter.isArraySorted()) {
                        //Thread.sleep(10);
                        sort(0,sorter.getLength()-1);

                        }


                    return sorter.getArray();
                }
                protected void process(List chunks)
                {
                    int[] val = (int[]) chunks.get(chunks.size()-1);
                    drafter.ChangeArray(val);
                    //drafter.repaint();

                }

                };
            sw1.execute();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM