Java学习-排序二叉树性能简单测试
1.创建4万个随机数,然后用分别用冒泡法,选择法,二叉树法3种排序算法进行排序,比较哪种更快
package Collection;
import java.util.ArrayList;
import java.util.List;
public class sortSpeedTest {
public static void main(String[] args) {
int num = 40000; // 元素个数
int rnd1[] = new int[num];
for (int i = 0; i < rnd1.length; i++) {
rnd1[i] = (int) Math.round(Math.random() * 100000);
}
int rnd2[] = new int[num];
int rnd3[] = new int[num];
System.arraycopy(rnd1, 0, rnd2, 0, rnd1.length);
System.arraycopy(rnd1, 0, rnd3, 0, rnd1.length);
long startTime1 = System.currentTimeMillis();
bubbleSort(rnd1);
long endTime1 = System.currentTimeMillis();
long startTime2 = System.currentTimeMillis();
selectSort(rnd2);
long endTime2 = System.currentTimeMillis();
long startTime3 = System.currentTimeMillis();
List res = binarySort(rnd3);
long endTime3 = System.currentTimeMillis();
System.out.printf("冒泡排序耗时:%d ms \n", (endTime1 - startTime1));
System.out.printf("选择排序耗时:%d ms \n", (endTime2 - startTime2));
System.out.printf("二叉树排序耗时:%d ms \n", (endTime3 - startTime3));
// System.out.println("验证rnd1");
// for(int x:rnd1)
// System.out.printf(x+" ");
// System.out.println("\n验证rnd2");
// for(int x:rnd2)
// System.out.printf(x+" ");
// System.out.println("\n验证rnd3");
// System.out.println(res);
}
public static class Node {
public Node LNode;
public Node RNode;
public Object value; // 结点的值
public void add(Object v) { // 传入的参数是要加入二叉树的新结点的值,是数值!!!
if (this.value == null) {
value = v;
} else {
if ((Integer) v > (Integer) value) { // 新增的结点的值大于当前结点的值
if (RNode == null) { // 当前结点右子树为空,要新建右子树结点
RNode = new Node(); // 使当前结点的右子树指向新增的结点,此时RNode的value自然没有赋值,是null
}
// 如果RNode非空,说明该结点右子树非空,则在右子树的基础上继续调用add以把数值v添加到二叉树正确的位置,
// 如果RNode为空,自然是上面新建右子树结点,并且由于此时RNode的value肯定是null,于是调用本身的add方法,把v赋值到RNode的value
RNode.add(v);
} else { // 新增的结点的值小于当前结点的值
if (LNode == null) {
LNode = new Node();
}
LNode.add(v);
}
}
}
// 二叉树的中序遍历,若二叉树本身是排序二叉树,则中序遍历能“有序输出”所有结点数值
public List<Object> values() { // 返回类型是List
List<Object> values = new ArrayList<Object>(); // 用来保存中途遍历的结果
if (LNode != null) {
values.addAll(LNode.values()); // 左子树非空,递归的“递”
}
values.add(value); // 把当前结点数值保存到values列表
if (RNode != null) {
values.addAll(RNode.values()); // 右子树非空,递归的“递”
}
return values; // 递归的“归”,先上层返回中途遍历结果
}
}
private static List binarySort(int[] rnd) {
// TODO Auto-generated method stub
Node root = new Node();
for (int x : rnd) {
root.add(x);
}
return root.values();
}
private static void selectSort(int[] rnd) {
// TODO Auto-generated method stub
int i, j, min, tmp, min_index;
for (i = 0; i < rnd.length; i++) {
min = 1000000;
min_index = i;
for (j = i; j < rnd.length; j++) {
if (min > rnd[j]) {
min = rnd[j];
min_index = j;
}
}
tmp = rnd[i];
rnd[i] = rnd[min_index];
rnd[min_index] = tmp;
}
}
private static void bubbleSort(int[] rnd) {
// TODO Auto-generated method stub
int i, j, tmp;
for (i = rnd.length - 1; i > 0; i--) {
for (j = 0; j + 1 < rnd.length; j++) {
if (rnd[j] > rnd[j + 1]) {
tmp = rnd[j];
rnd[j] = rnd[j + 1];
rnd[j + 1] = tmp;
}
}
}
}
}效果图:

验证语句取消注释后,简单测试100个数据元素的效果图:

完全正确~
相关推荐
zlxcsdn 2020-09-13
listep 2020-09-11
jokewinl 2020-07-18
liuyang000 2020-04-25
rein0 2020-04-18
wordmhg 2020-04-09
wangqing 2020-04-06
chaoxiao 2020-03-07
horizonheart 2020-02-16
adonislu 2020-02-14
sschencn 2020-02-14
嗡汤圆 2020-02-02
yuanran0 2020-01-30
wuxiaosi0 2020-01-08
yedaoxiaodi 2020-01-04
zhglinux 2020-01-03
程松 2020-01-01
蜗牛慢爬的李成广 2019-12-24