最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實(shí)現(xiàn)快速排序算法可視化的示例代碼

 更新時(shí)間:2022年08月23日 09:09:11   作者:天人合一peng  
快速排序算法通過(guò)多次比較和交換來(lái)實(shí)現(xiàn)排序,是對(duì)冒泡排序算法的一種改進(jìn)。本文將用Java語(yǔ)言實(shí)現(xiàn)快速排序算法并進(jìn)行可視化,感興趣的可以了解一下

實(shí)現(xiàn)效果

示例代碼

import java.awt.*;
 
public class AlgoVisualizer {
 
    private static int DELAY = 100;
 
    private SelectionSortData data;
    private AlgoFrame frame;
 
    public AlgoVisualizer(int sceneWidth, int sceneHeight, int N){
 
        data = new SelectionSortData(N, sceneHeight);
 
        // 初始化視圖
        EventQueue.invokeLater(() -> {
            frame = new AlgoFrame("Selection Sort Visualization", sceneWidth, sceneHeight);
            new Thread(() -> {
                run();
            }).start();
        });
    }
 
    private void run(){
        setData(0, -1, -1);
 
        for( int i = 0 ; i < data.N() ; i ++ ){
            // 尋找[i, n)區(qū)間里的最小值的索引
            int minIndex = i;
            setData(i, -1, minIndex);
            for( int j = i + 1 ; j < data.N() ; j ++ ){
                setData(i, j, minIndex);
                if( data.get(j) < data.get(minIndex) ){
                    minIndex = j;
                    setData(i, j, minIndex);
                }
            }
 
            data.swap(i , minIndex);
            setData(i +1 , -1, -1);
        }
        setData(data.N(), -1, -1);
    }
 
    private void setData(int orderedIndex, int currentCompareIndex, int currentMinIndex)
    {
        data.orderedIndex = orderedIndex;
        data.currentCompareIndex = currentCompareIndex;
        data.currentMinIndex = currentMinIndex;
 
        frame.render(data);
        AlgoVisHelper.pause(DELAY);
    }
 
    public static void main(String[] args) {
 
        int sceneWidth = 800;
        int sceneHeight = 800;
        int N = 100;
 
        AlgoVisualizer vis = new AlgoVisualizer(sceneWidth, sceneHeight, N);
    }
}
import java.awt.*;
import java.awt.geom.Ellipse2D;
 
import java.awt.geom.Rectangle2D;
import java.lang.InterruptedException;
 
public class AlgoVisHelper {
 
    private AlgoVisHelper(){}
 
    public static final Color Red = new Color(0xF44336);
    public static final Color Pink = new Color(0xE91E63);
    public static final Color Purple = new Color(0x9C27B0);
    public static final Color DeepPurple = new Color(0x673AB7);
    public static final Color Indigo = new Color(0x3F51B5);
    public static final Color Blue = new Color(0x2196F3);
    public static final Color LightBlue = new Color(0x03A9F4);
    public static final Color Cyan = new Color(0x00BCD4);
    public static final Color Teal = new Color(0x009688);
    public static final Color Green = new Color(0x4CAF50);
    public static final Color LightGreen = new Color(0x8BC34A);
    public static final Color Lime = new Color(0xCDDC39);
    public static final Color Yellow = new Color(0xFFEB3B);
    public static final Color Amber = new Color(0xFFC107);
    public static final Color Orange = new Color(0xFF9800);
    public static final Color DeepOrange = new Color(0xFF5722);
    public static final Color Brown = new Color(0x795548);
    public static final Color Grey = new Color(0x9E9E9E);
    public static final Color BlueGrey = new Color(0x607D8B);
    public static final Color Black = new Color(0x000000);
    public static final Color White = new Color(0xFFFFFF);
 
    public static void strokeCircle(Graphics2D g, int x, int y, int r){
 
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g.draw(circle);
    }
 
    public static void fillCircle(Graphics2D g, int x, int y, int r){
 
        Ellipse2D circle = new Ellipse2D.Double(x-r, y-r, 2*r, 2*r);
        g.fill(circle);
    }
 
    public static void strokeRectangle(Graphics2D g, int x, int y, int w, int h){
 
        Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
        g.draw(rectangle);
    }
 
    public static void fillRectangle(Graphics2D g, int x, int y, int w, int h){
 
        Rectangle2D rectangle = new Rectangle2D.Double(x, y, w, h);
        g.fill(rectangle);
    }
 
    public static void setColor(Graphics2D g, Color color){
        g.setColor(color);
    }
 
    public static void setStrokeWidth(Graphics2D g, int w){
        int strokeWidth = w;
        g.setStroke(new BasicStroke(strokeWidth, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
    }
 
    public static void pause(int t) {
        try {
            Thread.sleep(t);
        }
        catch (InterruptedException e) {
            System.out.println("Error sleeping");
        }
    }
 
}
import java.awt.*;
import javax.swing.*;
 
public class AlgoFrame extends JFrame{
 
    private int canvasWidth;
    private int canvasHeight;
 
    public AlgoFrame(String title, int canvasWidth, int canvasHeight){
 
        super(title);
 
        this.canvasWidth = canvasWidth;
        this.canvasHeight = canvasHeight;
 
        AlgoCanvas canvas = new AlgoCanvas();
        setContentPane(canvas);
        pack();
 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setResizable(false);
 
        setVisible(true);
    }
 
    public AlgoFrame(String title){
 
        this(title, 1024, 768);
    }
 
    public int getCanvasWidth(){return canvasWidth;}
    public int getCanvasHeight(){return canvasHeight;}
 
    // data
    private SelectionSortData data;
    public void render(SelectionSortData data){
        this.data = data;
        repaint();
    }
 
    private class AlgoCanvas extends JPanel{
 
        public AlgoCanvas(){
            // 雙緩存
            super(true);
        }
 
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
 
            Graphics2D g2d = (Graphics2D)g;
 
            // 抗鋸齒
            RenderingHints hints = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
            hints.put(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.addRenderingHints(hints);
 
            // 具體繪制
            int w = canvasWidth/data.N();
 
            for(int i = 0 ; i < data.N() ; i ++ ) {
                if( i < data.orderedIndex)
                    AlgoVisHelper.setColor(g2d, AlgoVisHelper.Red);
                else
                    AlgoVisHelper.setColor(g2d, AlgoVisHelper.Grey);
 
                if( i == data.currentCompareIndex)
                    AlgoVisHelper.setColor(g2d, AlgoVisHelper.Indigo);
                if(i == data.currentMinIndex)
                    AlgoVisHelper.setColor(g2d, AlgoVisHelper.Green);
 
                AlgoVisHelper.fillRectangle(g2d, i * w, canvasHeight - data.get(i), w - 1, data.get(i));
            }
        }
 
        @Override
        public Dimension getPreferredSize(){
            return new Dimension(canvasWidth, canvasHeight);
        }
    }
}
public class SelectionSortData {
 
    private int[] numbers;
    public int orderedIndex = -1; //[0,....,orderedIndex)是有序的
    public  int currentMinIndex = -1; //當(dāng)前找到的最小元素索引
    public  int currentCompareIndex = -1; //當(dāng)前正在比較的元素索引
 
    public SelectionSortData(int N, int randomBound){
 
        numbers = new int[N];
 
//        數(shù)據(jù)不能為0
        for( int i = 0 ; i < N ; i ++)
            numbers[i] = (int)(Math.random()*randomBound) + 1;
    }
 
    public int N(){
        return numbers.length;
    }
 
    public int get(int index){
        if( index < 0 || index >= numbers.length)
            throw new IllegalArgumentException("Invalid index to access Sort Data.");
 
        return numbers[index];
    }
 
    public void swap(int i, int j) {
 
        if( i < 0 || i >= numbers.length || j < 0 || j >= numbers.length)
            throw new IllegalArgumentException("Invalid index to access Sort Data.");
 
        int t = numbers[i];
        numbers[i] = numbers[j];
        numbers[j] = t;
    }
}

以上就是Java實(shí)現(xiàn)快速排序算法可視化的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于Java快速排序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Java使用FFmpeg處理視頻文件的方法教程

    Java使用FFmpeg處理視頻文件的方法教程

    這篇文章主要給大家介紹了關(guān)于Java使用FFmpeg處理視頻文件的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-03-03
  • logback如何自定義日志存儲(chǔ)

    logback如何自定義日志存儲(chǔ)

    這篇文章主要介紹了logback如何自定義日志存儲(chǔ)的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • 如何利用Spring的@Import擴(kuò)展點(diǎn)與spring進(jìn)行無(wú)縫整合

    如何利用Spring的@Import擴(kuò)展點(diǎn)與spring進(jìn)行無(wú)縫整合

    這篇文章主要介紹了如何利用Spring的@Import擴(kuò)展點(diǎn)與spring進(jìn)行無(wú)縫整合的實(shí)例代碼,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Java Socket編程實(shí)例(三)- TCP服務(wù)端線程池

    Java Socket編程實(shí)例(三)- TCP服務(wù)端線程池

    這篇文章主要講解Java Socket編程中TCP服務(wù)端線程池的實(shí)例,希望能給大家做一個(gè)參考。
    2016-06-06
  • Java隨機(jī)生成字符串的四種方式例子

    Java隨機(jī)生成字符串的四種方式例子

    這篇文章主要給大家介紹了關(guān)于Java隨機(jī)生成字符串的四種方式,隨機(jī)數(shù)在實(shí)際中使用很廣泛,比如要隨即生成一個(gè)固定長(zhǎng)度的字符串、數(shù)字,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下
    2023-10-10
  • 淺談Spring Boot日志框架實(shí)踐

    淺談Spring Boot日志框架實(shí)踐

    這篇文章主要介紹了淺談Spring Boot日志框架實(shí)踐,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • Java?List集合取交集的五種常見(jiàn)方式總結(jié)

    Java?List集合取交集的五種常見(jiàn)方式總結(jié)

    在Java中取兩個(gè)List集合的交集可以通過(guò)多種方式實(shí)現(xiàn),下面這篇文章主要給大家介紹了關(guān)于Java?List集合取交集的五種常見(jiàn)方式,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-07-07
  • Java實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語(yǔ)句的工具類

    Java實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語(yǔ)句的工具類

    這篇文章主要為大家詳細(xì)介紹了如何利用Java編寫(xiě)一個(gè)工具類,可以實(shí)現(xiàn)一鍵獲取Mysql所有表字段設(shè)計(jì)和建表語(yǔ)句,感興趣的小伙伴可以了解一下
    2023-05-05
  • SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)

    SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性)

    這篇文章主要介紹了SpringMVC中RequestMapping注解(作用、出現(xiàn)的位置、屬性),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-01-01
  • SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式

    SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式

    這篇文章主要介紹了SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05

最新評(píng)論

历史| 黎城县| 许昌市| 祁门县| 陆河县| 潼关县| 天祝| 汽车| 黎平县| 宣恩县| 遂宁市| 余姚市| 兰州市| 龙里县| 汕尾市| 关岭| 平乡县| 武隆县| 张家口市| 丰县| 伊通| 上虞市| 渭南市| 龙岩市| 兴和县| 奉贤区| 锡林郭勒盟| 黎川县| 天柱县| 图木舒克市| 梅州市| 峨山| 肃南| 留坝县| 达日县| 蕲春县| 宁国市| 丰县| 南靖县| 保山市| 西和县|