本篇主要为记录参加学校资讯班的作业,相关思考难点的纪录。
题目为比较4种sort演算法(Array-Sort、Insertion-Sort、Selection-Sort、Bubble-Sort)的执行速度,考虑不同排列会有不同的时间,本次设计取3次时间的平均值。
思考点:
import java.util.Arrays;
public class PerformanceBenchmarkforSortingAlgorithms {
public static void main(String[] args) {
int size[] = { 1000, 2000, 4000, 8000, 16000, 32000, 64000 };
System.out.print("Simulating ArraySort:");
double[] Amin = new double[size.length];
for (int times = 0; times < size.length; times++) {
int[] test = new int[size[times]];
double[] result = new double[3]; //同样的资料数量,算3次
for (int i = 0; i < 3; i++) {
test = sample(test); //reset资料变回乱数
double A000 = System.nanoTime()/1e6 ;
ArraySort(test); // 呼叫method-ArraySort
double A001 = System.nanoTime()/1e6 ;
result[i] = (A001 - A000); //将每一次的时间存起来
}
;
System.out.printf("."); //显示执行进度
Amin[times] = (result[0] + result[1] + result[2])/3; //3次平均
}
;
System.out.println("done");
System.out.print("Simulating InsertionSort:");
double[] Imin = new double[size.length];
for (int times = 0; times < size.length; times++) {
int[] test = new int[size[times]];
double[] result = new double[3];
for (int i = 0; i < 3; i++) {
test = sample(test);
double I000 = System.nanoTime()/1e6;
InsertionSort(test); // 呼叫method-InsertionSort
double I001 = System.nanoTime()/1e6;
result[i] = (I001 - I000);
}
;
System.out.printf(".");
Imin[times] = (result[0] / 3 + result[1] / 3 + result[2] / 3);
}
;
System.out.println("done");
System.out.print("Simulating SelectionSort:");
double[] Smin = new double[size.length];
for (int times = 0; times < size.length; times++) {
int[] test = new int[size[times]];
double[] result = new double[3];
for (int i = 0; i < 3; i++) {
test = sample(test);
double I000 = System.nanoTime()/1e6;
SelectionSort(test); // 呼叫method-SelectionSort
double I001 = System.nanoTime()/1e6;
result[i] = (I001 - I000);
}
;
System.out.printf(".");
Smin[times] = (result[0] / 3 + result[1] / 3 + result[2] / 3);
}
;
System.out.println("done");
System.out.print("Simulating BubbleSort:");
double[] Bmin = new double[size.length];
for (int times = 0; times < size.length; times++) {
int[] test = new int[size[times]];
double[] result = new double[3];
for (int i = 0; i < 3; i++) {
test = sample(test);
double I000 = System.nanoTime()/1e6;
BubbleSort(test); // 呼叫method-BubbleSort
double I001 = System.nanoTime()/1e6;
result[i] = (I001 - I000);
}
;
System.out.printf(".");
Bmin[times] = (result[0] / 3 + result[1] / 3 + result[2] / 3);
}
;
System.out.println("done");
System.out.println("Benchmark (time unit: ms)");
System.out.printf("%5s%23s%20s%20s%19s", "size", "BubbleSort", "SelectionSort", "InsertionSort", "ArraySort");
System.out.println();
for (int i = 0; i < size.length; i++) {
System.out.printf("%5d%20.3f%20.3f%20.3f%20.3f", size[i], (double) Bmin[i], (double) Smin[i],
(double) Imin[i], (double) Amin[i]);
System.out.println();
}
}
public static int[] sample(int[] num) { //生成乱数的排列
int[] a = num;
for (int i = 0; i < a.length; i++) {
a[i] = i;
}
;
for (int i = 0; i < a.length; i++) {
int j = (int) (Math.random() * (a.length - i) + i);
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
;
return a;
}
public static int[] ArraySort(int[] array01) {
int[] A = array01;
Arrays.sort(A);
return A;
}
public static int[] BubbleSort(int[] array01) {
int[] A = array01;
boolean swapped;
do {
swapped = false;
for (int i = 0; i < A.length - 1; i++) {
if (A[i] > A[i + 1]) {
int temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
swapped = true;
}
;
}
;
} while (swapped);
return A;
}
public static int[] SelectionSort(int[] array01) {
int[] A = array01;
for (int j = 0; j < A.length; j++) {
int min = j;
for (int i = 0; i < A.length; i++) {
if (A[min] > A[i]) {
min = i;
}
;
int temp;
temp = A[j];
A[j] = A[min];
A[min] = temp;
}
;
}
;
return A;
}
public static int[] InsertionSort(int[] array01) {
int[] A = array01;
for (int i = 0; i < A.length; i++) {
int index = i;
while (index > 0 && A[index - 1] >= A[index]) {
int tmp = A[index - 1];
A[index - 1] = A[index];
A[index] = tmp;
index -= 1; //继续向左边比大小;当index为0的时候,代表已经排到最小了,loop停止
}
}
return A;
}
}
参考文章:
>>: 《中国哲学书电子化计划》[简单修改模式]下图文对照文本输入辅助工具
#odoo #开源系统 #数位赋能 #E化自主 企业之广告行销手法,除了透过email、UTMs、问...
唯心又看了诗忆之前写的几个高阶函式练习。「嗯⋯⋯我觉得你与其说是对高阶函式不熟,不如说是对匿名函式不...
甚麽是指标(Pointer)? 指标可以拿来存取电脑的记忆体位址,所以,我们在使用指标变数之前,要先...
上篇回顾 设定档格式 YAML Docker太多文章介绍了, 小弟我K8S也没那麽熟稔 就介绍自己熟...