无聊 做个测试给大家分享下 (冒泡排序和选择排序)
package com.jeff.util;import java.util.ArrayList;
import java.util.List;
public class SortTest {
// 测试数据大小
private int sumint;
private int[] arr;
private List<Integer> list;
/**
* 使用构造函数初始化测试数据分别以指定数据大小生成Array和List
*/
public SortTest() {
this.sumint = 1000;// 默认为一千
this.arr = new int[sumint];
this.list = new ArrayList<Integer>();
for (int i = 0; i < 1000; i++) {
int flag = (int) (Math.random() * 1000);
this.arr[i] = flag;
this.list.add(flag);
}
}
public SortTest(int sum) {
if (sum < 0)
sum = 1000;// 不合法数据归于默认值
this.sumint = sum;
this.arr = new int[sumint];
this.list = new ArrayList<Integer>();
for (int i = 0; i < sum; i++) {
int flag = (int) (Math.random() * sum);
this.arr[i] = (int) flag;
this.list.add(flag);
}
}
public void maopao() {
long start = System.currentTimeMillis();
System.out.println("冒泡法排序开始:" + start);
for (int i = 0; i < this.arr.length - 1; i++) {
for (int j = 0; j < this.arr.length - i - 1; j++) {
if (this.arr[j] > this.arr[j + 1]) {
int temp = this.arr[j];
this.arr[j] = this.arr[j + 1];
this.arr[j + 1] = temp;
}
}
}
long end = System.currentTimeMillis();
System.out.println("冒泡排序结束:" + end);
System.out.println("冒泡用时:" + (end - start) + "毫秒");
// for (int i = 0; i < this.arr.length; i++) {
// System.out.print(this.arr[i] + ", ");
//
// }
System.out.println("===========List start=============");
start = System.currentTimeMillis();
System.out.println("冒泡法排序list开始:" + start);
for (int i = 0; i < this.list.size() - 1; i++) {
for (int j = 0; j < this.list.size() - i - 1; j++) {
if (this.list.get(j) > this.list.get(j + 1)) {
int temp = this.list.get(j);
this.list.set(j, this.list.get(j + 1));
this.list.set(j + 1, temp);
}
}
}
end = System.currentTimeMillis();
System.out.println("冒泡排序list结束:" + end);
System.out.println("list冒泡用时:" + (end - start) + "毫秒");
}
public void selectSort() {
System.out.println("+++++++++++++++++++++++++++++++++++");
long start = System.currentTimeMillis();
System.out.println("数组选择排序开始:" + start);
int minnum;// 记录最小数据
int minindex;// 记录最小数据索引
for (int i = 0; i < this.arr.length; i++) {
minnum = this.arr[i];// 取当前第一个数为参考
minindex = i;
for (int j = i; j < this.arr.length; j++) {
if (minnum > this.arr[j]) {
minnum = this.arr[j];
minindex=j;
}
}// 结束内for找到最小数
if (minindex != i) {
int temp = this.arr[i];
this.arr[i]=this.arr[minindex];
this.arr[minindex] = temp;
}
}
long end = System.currentTimeMillis();
System.out.println("数组选择排序结束:" + end);
System.out.println("数组选择用时:" + (end - start) + "毫秒");
System.out.println("============List选择排序开始=========");
start = System.currentTimeMillis();
System.out.println("数组选择排序开始:" + start);
for (int i = 0; i < this.list.size(); i++) {
minnum = this.list.get(i);// 取当前第一个数为参考
minindex = i;
for (int j = i; j < this.list.size(); j++) {
if (minnum > this.list.get(j)) {
minnum = this.list.get(j);
minindex=j;
}
}// 结束内for找到最小数
if (minindex != i) {
int temp = this.list.get(i);
this.list.set(i, this.list.get(minindex));
this.list.set(minindex, temp);
}
}
end = System.currentTimeMillis();
System.out.println("List选择排序结束:" + end);
System.out.println("List选择排序用时:" + (end - start) + "毫秒");
// System.out.println(this.list.toString());
}
public static void main(String[] args) {
SortTest st = new SortTest(1000);
st.maopao();
SortTest st1 = new SortTest(1000);
st1.selectSort();
}
}