首页 技术 正文
技术 2022年11月6日
0 收藏 528 点赞 336 浏览 1569 个字

先给代码,再给过程视频:

 package com.dyi.wyb.sort; import java.awt.Color;
import java.awt.Graphics;
import java.util.Random; import javax.swing.*; public class InsertionSort extends JFrame {
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* main method
*
* @param args
* []
* @author stagebo
*/
static int[] arr = getArray(1000);
static InsertionSort show;
public static void main(String[] args) {
show=new InsertionSort("插入排序");
insertionSort(arr);
} /**
* introduction:algorithms of insertionSort
*
* @param arr
* []
* @return void
*
*/
public static void insertionSort(int[] arr) {
for (int j = 1; j < arr.length; j++) {
int key = arr[j];
int i;
for (i = j - 1; i >= 0 && arr[i] > key; i--) {
arr[i + 1] = arr[i];
try{Thread.sleep(5);}catch(Exception e){}
show.repaint();
}
arr[i + 1] = key;
}
} /**
* function: print array
*
* @param arr
* @param str
*/
public static void printArray(int[] arr, String str) {
System.out.print(str + ":");
for (int i : arr) {
System.out.print(i + "--");
}
System.out.println();
} /**
* constructor,initial the panel
*/
public InsertionSort(String title) {
setTitle(title);
setLocation(20, 20);
setSize(1000, 600);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public void paint(Graphics g) {
for (int i = 0; i < arr.length; i++) {
g.setColor(Color.BLACK);
g.drawLine(i, 600, i, 600-arr[i]);
g.setColor(Color.WHITE);
g.drawLine(i, 0,i, 600-arr[i]);
}
} /**
* return a random value array
*
* @param length
* @return array[length]
*/
public static int[] getArray(int length) {
int[] re = new int[length];
for (int i = 0; i < re.length; i++)
re[i] = i / 2;
for (int i = 0; i < re.length; i++) {
int index1 = new Random().nextInt(length);
for (int j = 0; j < 3; j++) {
int temp = re[i];
re[i] = re[index1];
re[index1] = temp;
}
}
return re;
}
}

插入排序以及显示面板代码

插图,插入排序过程显示视频连接

【开发者笔记】插入排序过程呈现之java内置GUI表示

插入排序的时间复杂度T(n)=O(n2),和冒泡排序半斤八两。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,999
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,511
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,357
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,140
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,770
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,848