要求:随机生成排序所用100000个实数,生成的数以数组的形式存储
记录各排序方法所需排序时间
重复进行3次运算后取出平均耗时做比较
以柱状图的形式展示结果,分析结果
主程序:
1 import java.util.Random;
2 import javax.swing.JFrame;
3
4 public class test extends JFrame{
5
6 //随机100000个生成数据
7 public static double[] data() {
8 Random random = new Random();
9 double[] arr=new double[100000];
10 for(int i=0;i<100000;i++) {
11 arr[i]=random.nextDouble();
12 }
13 return arr;
14 }
15
16 //冒泡排序
17 public static void bubbleSort(double[] arr) {
18 double temp;//定义一个临时变量
19 for(int i=0;i<arr.length-1;i++){//趟数
20 for(int j=0;j<arr.length-i-1;j++){
21 //若后面个数据小,则交换两个元素
22 if(arr[j+1]<arr[j]){
23 temp = arr[j];
24 arr[j] = arr[j+1];
25 arr[j+1] = temp;
26 }
27 }
28 }
29 }
30
31 //选择排序
32 public static void selectionSort(double[] arr) {
33 for(int i=0; i<arr.length; i++) {
34 int minIndex = i;//最小数索引
35 for(int j=i; j<arr.length; j++) {
36 //找最小数,并记录最小数的索引
37 if(arr[j] < arr[minIndex]) {
38 minIndex = j;
39 }
40 }
41 //交换符合条件的数
42 double tmp = arr[i];
43 arr[i] = arr[minIndex];
44 arr[minIndex] = tmp;
45 }
46 }
47
48 //插入排序
49 public static void insertionSort(double[] arr) {
50 int j=0;
51 for(int i=1;i<arr.length;i++) {
52 double temp=arr[i]; //temp存放将要插入的数据
53 if(arr[i]<arr[i-1]) {
54 for( j=i-1;j>=0&&arr[j]>temp;j--) {
55 arr[j+1]=arr[j];
56 //temp的位置空出来,把第j个的值赋给第j+1个;
57 }
58 arr[j+1]=temp;
59 }
60 }
61 }
62
63 //自底向上归并排序
64 public static void sortDownToUp(double[] arr) {
65 double[] aux = new double[arr.length];
66 for (int sz = 1; sz < arr.length; sz *= 2) {
67 for (int start = 0; start < arr.length - sz; start += 2 * sz) {
68 int end=Math.min(start + 2 * sz - 1, arr.length - 1);
69 int mid=start + sz - 1;
70 //按照长度归并回arr数组,归并后长度翻倍
71 for (int k = start; k <= end; k++) {
72 aux[k] = arr[k];
73 }
74 int i = start, j = mid + 1;
75 //[start,mid] [mid+1, end]
76 for (int k = start; k <= end; k++) {
77 if (i > mid) {
78 arr[k] = aux[j++];
79 } else if (j > end) {
80 arr[k] = aux[i++];
81 } else if (aux[j] < aux[i]) {
82 arr[k] = aux[j++];
83 } else {
84 arr[k] = aux[i++];
85 }
86 }
87 }
88 }
89 }
90
91 // 主函数
92 public static void main(String[] args) {
93 // TODO Auto-generated method stub
94
95 double starttime,endtime;
96 double[] arr=data();//生成数据
97
98 //3次运行对比
99 double[] b_time=new double[3];
100 double[] s_time=new double[3];
101 double[] i_time=new double[3];
102 double[] d_time=new double[3];
103 double[] avartime=new double[4];//平均运行时间
104
105 for(int i=0;i<3;i++) {
106 double[] b_arr=arr.clone();
107 double[] s_arr=arr.clone();
108 double[] i_arr=arr.clone();
109 double[] d_arr=arr.clone();
110
111 starttime=System.currentTimeMillis();
112 bubbleSort(b_arr);//冒泡排序
113 endtime=System.currentTimeMillis();
114 b_time[i]=(endtime-starttime)/1000.0;//耗时
115
116 starttime=System.currentTimeMillis();
117 selectionSort(s_arr);//选择排序
118 endtime=System.currentTimeMillis();
119 s_time[i]=(endtime-starttime)/1000.0;//耗时
120
121 starttime=System.currentTimeMillis();
122 insertionSort(i_arr);//插入排序
123 endtime=System.currentTimeMillis();
124 i_time[i]=(endtime-starttime)/1000.0;//耗时
125
126 starttime=System.currentTimeMillis();
127 sortDownToUp(d_arr);//自底向上归并排序
128 endtime=System.currentTimeMillis();
129 d_time[i]=(endtime-starttime)/1000.0;//耗时
130 }
131 //平均运行时间
132 avartime[0]=(b_time[0]+b_time[1]+b_time[2])/3;
133 avartime[1]=(s_time[0]+s_time[1]+s_time[2])/3;
134 avartime[2]=(i_time[0]+i_time[1]+i_time[2])/3;
135 avartime[3]=(i_time[0]+i_time[1]+i_time[2])/3;
136
137 System.out.println("冒泡排序\t选择排序\t插入排序\t归并排序");
138 for(int j=0;j<3;j++) {
139 System.out.println(b_time[j]+"s\t"+s_time[j]+"s\t"+i_time[j]+"s\t"+d_time[j]+"s");
140 }
141 System.out.println("平均时间:");
142
143 System.out.println(avartime[0]+"s\t"+avartime[1]+"s\t"+avartime[2]+"s\t"+avartime[3]+"s\t");
144
145
146
147 }
148
149 }
运行结果(每次运行都不同):

可视化图表程序(Echarts图表库):
1 var posList = [
2 'left', 'right', 'top', 'bottom',
3 'inside',
4 'insideTop', 'insideLeft', 'insideRight', 'insideBottom',
5 'insideTopLeft', 'insideTopRight', 'insideBottomLeft', 'insideBottomRight'
6 ];
7
8 app.configParameters = {
9 rotate: {
10 min: -90,
11 max: 90
12 },
13 align: {
14 options: {
15 left: 'left',
16 center: 'center',
17 right: 'right'
18 }
19 },
20 verticalAlign: {
21 options: {
22 top: 'top',
23 middle: 'middle',
24 bottom: 'bottom'
25 }
26 },
27 position: {
28 options: echarts.util.reduce(posList, function (map, pos) {
29 map[pos] = pos;
30 return map;
31 }, {})
32 },
33 distance: {
34 min: 0,
35 max: 100
36 }
37 };
38
39 app.config = {
40 rotate: 90,
41 align: 'left',
42 verticalAlign: 'middle',
43 position: 'insideBottom',
44 distance: 15,
45 onChange: function () {
46 var labelOption = {
47 normal: {
48 rotate: app.config.rotate,
49 align: app.config.align,
50 verticalAlign: app.config.verticalAlign,
51 position: app.config.position,
52 distance: app.config.distance
53 }
54 };
55 myChart.setOption({
56 series: [{
57 label: labelOption
58 }, {
59 label: labelOption
60 }, {
61 label: labelOption
62 }, {
63 label: labelOption
64 }]
65 });
66 }
67 };
68
69 var labelOption = {
70 normal: {
71 show: true,
72 position: app.config.position,
73 distance: app.config.distance,
74 align: app.config.align,
75 verticalAlign: app.config.verticalAlign,
76 rotate: app.config.rotate,
77 formatter: '{c} {name|{a}}',
78 fontSize: 16,
79 rich: {
80 name: {
81 textBorderColor: '#fff'
82 }
83 }
84 }
85 };
86
87 option = {
88 color: ['#003366', '#006699', '#4cabce', '#e5323e'],
89 tooltip: {
90 trigger: 'axis',
91 axisPointer: {
92 type: 'shadow'
93 }
94 },
95 legend: {
96 data: ['Forest', 'Steppe', 'Desert', 'Wetland']
97 },
98 toolbox: {
99 show: true,
100 orient: 'vertical',
101 left: 'right',
102 top: 'center',
103 feature: {
104 mark: {show: true},
105 dataView: {show: true, readOnly: false},
106 magicType: {show: true, type: ['line', 'bar', 'stack', 'tiled']},
107 restore: {show: true},
108 saveAsImage: {show: true}
109 }
110 },
111 calculable: true,
112 xAxis: [
113 {
114 type: 'category',
115 axisTick: {show: false},
116 data: ['冒泡排序', '选择排序', '插入排序', '归并排序']
117 }
118 ],
119 yAxis: [
120 {
121 type: 'value'
122 }
123 ],
124 series: [
125 {
126 name: '第一次',
127 type: 'bar',
128 barGap: 0,
129 label: labelOption,
130 data: [28.834, 6.102, 1.32, 0.024]
131 },
132 {
133 name: '第二次',
134 type: 'bar',
135 label: labelOption,
136 data: [24.789, 7.305, 1.21, 0.012]
137 },
138 {
139 name: '第三次',
140 type: 'bar',
141 label: labelOption,
142 data: [19.88, 2.661, 2.412,0.023]
143 },
144 {
145 name: '平均时间',
146 type: 'bar',
147 label: labelOption,
148 data: [24.501, 5.356, 1.6473]
149 }
150 ]
151 };
图表:

优缺点比较:
1.胃泡排序
(1)基本原理
在要排序的一-组数中,对当前还未排好序的范围内的全部数,自上而下对相邻的两个数依次进行比较,让
较大的数往下沉,较小的往上冒。即:每当两相邻的数比较后发现他们的排序与排序要求相反时,就将他们互换。
(2)优缺点
优点:稳定性小
缺点:慢,每次只能移动两个相邻的数据;
2.简单选择排序
(1)基本思想
第一趟:从第一个记录开始,将后面n-1个记录进行比较,找到其中最小的记录和第一一个记录进行交换;
第二趟:从第二个记录开始,将后面n-2个记录进行比较,找到其中最小的记录和第2个记录进行交换;
第i趟:从第i个记录开始,将后面n-i个记录进行比较,找到其中最小的记录和第i个记录进行交换;
以此类推,经过n-1趟比较,将n-1个记录排到位,剩下一个最大记录直接排在最后;
3.插入排序4
(1)基本思想
将一个记录插入到已排序好的有序表中,从而得到一个新的,记录数增1的有序表。即先将序列的第一个
记录看成是一个有序的子序列,然后从第二个记录逐个进行插入,直至整个序列有序为止。
(2)优缺点
优点:稳定,快
缺点:比较次数不一定,比较次数越少,插入点后的数据移动越多,特别是数据量庞大的时候
4.归并排序
(1)基本思想
首先将初始序列的n个记录看成是n个有序的子序列,每个子序列的长度为1,然后两两归并,得到n/2个
长度为2的有序子序列,在此基础上,再对长度为2的有序子序列进行两两归并,得到若干个长度为4的
有序子序列,以此类推,直到得到一个长度为n的有序序列为止;小
(2)适用场景
若n较大,并且要求排序稳定,则可以选择归并排序;