zno2

jfreechart 排序算法动图

JFreeChart is a free 100% Java chart library that makes it easy for developers to display professional quality charts in their applications. 

 

https://www.jfree.org/jfreechart/

 

        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreechart</artifactId>
            <version>1.5.0</version>
        </dependency>

        <dependency>
            <groupId>com.object-refinery</groupId>
            <artifactId>orsoncharts</artifactId>
            <version>1.7</version>
        </dependency>

        <dependency>
            <groupId>org.jfree</groupId>
            <artifactId>jfreesvg</artifactId>
            <version>3.3</version>
        </dependency>

        <dependency>
            <groupId>com.orsonpdf</groupId>
            <artifactId>orsonpdf</artifactId>
            <version>1.7</version>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.8.5</version>
        </dependency>

 

import java.awt.Color;
import java.awt.Dimension;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.block.BlockBorder;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.chart.ui.ApplicationFrame;
import org.jfree.chart.ui.UIUtils;
import org.jfree.data.category.DefaultCategoryDataset;


public class BarChartDemo extends ApplicationFrame {

    private static final long serialVersionUID = 1L;
    
    private static final long slp = 100L;

    static DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    

    public BarChartDemo(String title, DefaultCategoryDataset dataset) {
        super(title);

        JFreeChart chart = ChartFactory.createBarChart("", null, "", dataset);
        chart.addSubtitle(new TextTitle(""));
        chart.setBackgroundPaint(Color.WHITE);
        CategoryPlot plot = (CategoryPlot) chart.getPlot();

        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
        BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false);
        chart.getLegend().setFrame(BlockBorder.NONE);

        ChartPanel chartPanel = new ChartPanel(chart, false);
        chartPanel.setFillZoomRectangle(false);
        chartPanel.setMouseWheelEnabled(false);
        chartPanel.setPreferredSize(new Dimension(1500, 1100));
        setContentPane(chartPanel);
    }
    

    public static void main(String[] args) throws InterruptedException {

        BarChartDemo demo = new BarChartDemo("排序算法", dataset);
        demo.pack();
        UIUtils.centerFrameOnScreen(demo);
        int[] arr = cn.hutool.core.util.RandomUtil.randomInts(150);

        init(arr,demo);

        quickSort(arr, "asc");
//        bub1(arr, "asc");
//        bub2(arr, "asc");
//        insert(arr, "asc");
//        select(arr, "asc");

        



    }

    static void setValue(int key, int value) {
        dataset.setValue(value, "", key+"");
    }
    
    static void exchange(int arr[], int a, int av, int b , int bv) throws InterruptedException {
        Thread.sleep(slp);
        arr[a] = av;
        setValue(a,av);
        arr[b] = bv;
        setValue(b,bv);
    }
    
    static void init(int[] arr, java.awt.Window w) {
        for(int i=0;i<arr.length;i++) {
            setValue(i, arr[i]);
        }
        w.setVisible(true);
    }
    
     public static void quickSort(int arr[], String sortType) throws InterruptedException {
            quickSort(arr, 0, arr.length - 1, sortType);
        }

        // start vs end
        // low vs high
        // left vs right
        // 充分利用数组空间 (不管如何交换,数据都在)
        // 递归 
        private static void quickSort(int[] arr, int l, int h, String t) throws InterruptedException {
            if (l >= h) {
                return;
            }
            int s = l;
            int e = h;
            boolean asc = "asc".equalsIgnoreCase(t);
            int b = arr[s];
            while (s < e) {
                for (; s < e; e--) {
                    if (asc ? b > arr[e] : b < arr[e]) {
                        exchange(arr, s, arr[e], e, b);
                        break;
                    }
                }
                for (; s < e; s++) {
                    if (asc ? b < arr[s] : b > arr[s]) {
                        exchange(arr, e, arr[s], s, b);
                        break;
                    }
                }
            }
            quickSort(arr, l, s - 1, t);
            quickSort(arr, s + 1, h, t);
        }
    
      /**
     * 冒泡排序
     * 
     * @param arr
     * @param flag
     * @throws InterruptedException 
     */
    public static void bub1(int[] arr, String flag) throws InterruptedException {

        boolean asc = "asc".equalsIgnoreCase(flag);
        boolean exchanged = false;
        do {
            exchanged = false;
            for (int i = 0; i < arr.length - 1; i++) {
                int a = arr[i];
                int b = arr[i + 1];
                if (asc ? a > b : a < b) {
                    exchange(arr, i, b, i + 1, a);
                    exchanged = true;
                }else {
                    Thread.sleep(slp);
                }
            }
        } while (exchanged);
    }
    /**
     * 冒泡排序-尾部未发生排序的部分截除
     * 
     * @param arr
     * @param flag
     * @throws InterruptedException 
     */
    public static void bub2(int[] arr, String flag) throws InterruptedException {

        boolean asc = "asc".equalsIgnoreCase(flag);
        boolean exchanged = false;
        int f = arr.length - 1;
        do {
            exchanged = false;
            int e = f;
            for (int i = 0; i < e; i++) {
                int a = arr[i];
                int b = arr[i + 1];
                if (asc ? a > b : a < b) {
                    arr[i] = b;
                    arr[i + 1] = a;
                    f = i;
                    exchange(arr, i, b, i + 1, a);
                    exchanged = true;
                }else {
                    Thread.sleep(slp);
                }
            }
        } while (exchanged);
    }
    
    /**
     * 插入排序
     * 
     * @param arr
     * @param flag
     * @throws InterruptedException 
     */
    public static void insert(int[] arr, String flag) throws InterruptedException {

        boolean asc = "asc".equalsIgnoreCase(flag);
        for (int i = 1; i < arr.length; i++) {
            for (int j = i; j > 0; j--) {
                int a = arr[j - 1];
                int b = arr[j];
                if (asc ? a > b : a < b) {
                    exchange(arr, j - 1, b, j, a);
                } else {
                    break;
                }
            }
        }
    }
    
    /**
     * 选择排序
     * 
     * @param arr
     * @param flag
     * @throws InterruptedException 
     */
    public static void select(int[] arr, String flag) throws InterruptedException {

        boolean asc = "asc".equalsIgnoreCase(flag);
        for (int j = 0; j < arr.length - 1; j++) {
            for (int i = j + 1; i < arr.length; i++) {
                int a = arr[j];
                int b = arr[i];
                if (asc ? a > b : a < b) {
                    exchange(arr, j, b, i, a);
                }
            }
        }
    }
    


}

 

冒泡排序升序

冒泡排序升序(优化)

选择排序升序

插入排序升序

 

快速排序(放慢100倍)

快速排序升序正常速,10倍数据量

 

快速排序降序正常速,10倍数据量

 

小结:快速排序吊打冒泡、插入、选择

 

posted on 2023-06-02 08:08  zno2  阅读(7)  评论(0编辑  收藏  举报

导航