package MyPackage;
import java.util.Arrays;
import java.util.Random;
public class ArraySortPK {
public static void main(String[] args) {
//数组原料准备
int n = 20000;
int[] a = new int[n];
Random ran = new Random();
a[i] = ran.nextInt();
}
//复制数组
int[] ac = Arrays.copyOf(a,a.length);
int[] ab = Arrays.copyOf(a,a.length);
int[] ai = Arrays.copyOf(a,a.length);
//调用排序方法,测出时间
long time=tongjiAPI(a);
long timec=tongjiChoose(ac);
long timeb=tongjiBubble(ab);
long timei=tongjiInsert(ai);
//打印时间
System.out.println("使用java API排序需要消耗的时间为:"+time);
System.out.println("使用choose排序需要消耗的时间为:"+timec);
System.out.println("使用bubble排序需要消耗的时间为:"+timeb);
System.out.println("使用insert排序需要消耗的时间为:"+timei);
}
private static long tongjiAPI(int[] ary) {
long start=System.currentTimeMillis();
Arrays
.
sort(ary);
long end=System.currentTimeMillis();
return end-start;
}
private static long tongjiInsert(int[] aryi) {
long start=System.currentTimeMillis();
ArraySort
.
insertSort(aryi);
long end=System.currentTimeMillis();
return end-start;
}
private static long tongjiBubble(int[] aryb) {
long start=System.currentTimeMillis();
ArraySort
.
bubbleSort(aryb);
long end=System.currentTimeMillis();
return end-start;
}
private static long tongjiChoose(int[] aryc) {
long start=System.currentTimeMillis();
ArraySort
.
chooseSort(aryc);
long end=System.currentTimeMillis();
return end-start;
}
}
####################调用的
ArraySort类
##############################
package MyPackage;
import java.util.Arrays;
public class
ArraySort{
public static void main(String[] args) {
}
//chooseSort
public static void chooseSort(int[] aryc) {
// TODO Auto-generated method stub
if(aryc[i]>aryc[j]) {
int temp = aryc[i];
aryc[i] = aryc[j];
aryc[j] = temp;
}
}
}
//System.out.println(Arrays.toString(aryc));
}
//bubbleSort
public static void bubbleSort(int[] aryb) {
// TODO Auto-generated method stub
if(aryb[i]>aryb[j]) {
int temp = aryb[i];
aryb[i] = aryb[j];
aryb[j] = temp;
}
}
}
//System.out.println(Arrays.toString(aryb));
}
//insertSort
public static void insertSort(int[] aryi) {
// TODO Auto-generated method stub
int temp=aryi[t];
for(int i=t-1;i>=0;i--){
aryi[i+1]=aryi[i];
aryi[i] = temp;
}
}
}
//System.out.println(Arrays.toString(aryi));
}
}