import java.awt.List;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Random;
/**
*@作者:Flyige
*@时间:2018/12/1
*@类名称:SolutionsForSort
*/
public class SolutionsForSort {
//冒泡排序算法,简单不解释
public static void BubbleSort(int ele[]) {
for (int i = 0; i < ele.length; i++) {
for (int j = i + 1; j < ele.length; j++) {
if (ele[i] > ele[j]) {
int temp = ele[i];
ele[i] = ele[j];
ele[j] = temp;
}
}
}
System.out.println("冒泡排序:");
for (int i : ele) {
System.out.print(i + " ");
}
System.out.println();
}
//选择排序算法,找小的往前按顺序放。
public static void SelectionSort(int ele[]) {
int min = 0;//用来存放每次遍历数组中的最小值
for (int i = 0; i < ele.length; i++) {
min = i;
for (int j = i + 1; j < ele.length; j++) {
if (ele[min] > ele[j])
min = j;
}
int temp = ele[i];
ele[i] = ele[min];
ele[min] = temp;
}
System.out.println("选择排序:");
for (int i : ele) {
System.out.print(i + " ");
}
System.out.println();
}
//插入排序,很奇妙的思路,把数组一分为二(不等分),前者为顺序数组,后者为等待比较数组
public static void InsertSort(int ele[]) {
if (ele == null || ele.length < 2) {//数组为空或者长度不足2,直接退出
return;
}
for (int i = 1; i < ele.length - 1; i++) {
int currentvalue = ele[i];//右侧列表的值,按顺序取
int position = i;//表示要把当前值放置到的位置,也可以看作是当前值去比较的位置
for (int j = i - 1; j >= 0; j--) {//相当与是反向的遍历一个数组
if (ele[j] > currentvalue) {//如果前一个比后一个大,那就把前一个值得位置往后移,把小值得位置留出来
ele[j + 1] = ele[j];
position -= 1;//因为这个值比右侧列表的当前循环值大,所以比较下一位。
} else {
break;
}
}
ele[position] = currentvalue;//最后比较到了这个值的位置后放入当前值
}
System.out.println("插入排序:");
for (int i : ele) {
System.out.print(i + " ");
}
System.out.println();
}
public static void main(String[] args) throws IOException {
System.out.println("数据规模比较开始,本次实验单位:毫秒");
System.out.println("按回车继续");
new BufferedReader(new InputStreamReader(System.in)).readLine();
Random r = new Random();
int eleOf10[] = new int[10];
int eleOf100[] = new int[100];
int eleOf1000[] = new int[1000];
int eleOf10000[] = new int[10000];
System.out.println("正在生成随机数数组...");
for (int i = 0; i < 10; i++) {
eleOf10[i] = r.nextInt();
}
for (int i = 0; i < 100; i++) {
eleOf100[i] = r.nextInt();
}
for (int i = 0; i < 1000; i++) {
eleOf1000[i] = r.nextInt();
}
for (int i = 0; i < 10000; i++) {
eleOf10000[i] = r.nextInt();
}
System.out.println("随机数组生成完毕");
System.out.println("按回车继续");
new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("————————————————————冒泡排序开始:");
long BstartTime1 = System.currentTimeMillis();
BubbleSort(eleOf10);
long BendTime1 = System.currentTimeMillis();
System.out.println("数据规模:10;冒泡排序所用时间为:" + (BendTime1 - BstartTime1));
long BstartTime2 = System.currentTimeMillis();
BubbleSort(eleOf100);
long BendTime2 = System.currentTimeMillis();
System.out.println("数据规模:100;冒泡排序所用时间为:" + (BendTime2 - BstartTime2));
long BstartTime3 = System.currentTimeMillis();
BubbleSort(eleOf1000);
long BendTime3 = System.currentTimeMillis();
System.out.println("数据规模:1000;冒泡排序所用时间为:" + (BendTime3 - BstartTime3));
long BstartTime4 = System.currentTimeMillis();
BubbleSort(eleOf10000);
long BendTime4 = System.currentTimeMillis();
System.out.println("数据规模:10000;冒泡排序所用时间为:" + (BendTime4 - BstartTime4));
System.out.println("按回车继续");
new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("————————————————————选择排序开始:");
long SstartTime1 = System.currentTimeMillis();
SelectionSort(eleOf10);
long SendTime1 = System.currentTimeMillis();
System.out.println("数据规模:10;选择排序所用时间为:" + (SendTime1 - SstartTime1));
long SstartTime2 = System.currentTimeMillis();
SelectionSort(eleOf100);
long SendTime2 = System.currentTimeMillis();
System.out.println("数据规模:100;选择排序所用时间为:" + (SendTime2 - SstartTime2));
long SstartTime3 = System.currentTimeMillis();
SelectionSort(eleOf1000);
long SendTime3 = System.currentTimeMillis();
System.out.println("数据规模:1000;选择排序所用时间为:" + (SendTime3 - SstartTime3));
long SstartTime4 = System.currentTimeMillis();
SelectionSort(eleOf10000);
long SendTime4 = System.currentTimeMillis();
System.out.println("数据规模:10000;选择排序所用时间为:" + (SendTime4 - SstartTime4));
System.out.println("按回车继续");
new BufferedReader(new InputStreamReader(System.in)).readLine();
System.out.println("————————————————————插入排序开始:");
long IstartTime1 = System.currentTimeMillis();
InsertSort(eleOf10);
long IendTime1 = System.currentTimeMillis();
System.out.println("数据规模:10;插入排序所用时间为:" + (IendTime1 - IstartTime1));
long IstartTime2 = System.currentTimeMillis();
InsertSort(eleOf100);
long IendTime2 = System.currentTimeMillis();
System.out.println("数据规模:100;插入排序所用时间为:" + (IendTime2 - IstartTime2));
long IstartTime3 = System.currentTimeMillis();
InsertSort(eleOf1000);
long IendTime3 = System.currentTimeMillis();
System.out.println("数据规模:1000;插入排序所用时间为:" + (IendTime3 - IstartTime3));
long IstartTime4 = System.currentTimeMillis();
InsertSort(eleOf10000);
long IendTime4 = System.currentTimeMillis();
System.out.println("数据规模:10000;插入排序所用时间为:" + (IendTime4 - IstartTime4));
}
}
运行截图: