聪聪工作室---JAVA入门小程序---难题小程序
import java.util.Arrays;
import java.util.Random;
public class Insertp {
public static void main(String[] args) {
// TODO Auto-generated method stub
int temp;
Random random=new Random();
int[] a=new int[3000];
long startTime=System.currentTimeMillis();
for (int i = 0; i < a.length; i++) {
a[i]=random.nextInt(3000);
}
long endTime=System.currentTimeMillis();
System.out.print("生成随机数组所用的时间为:"+"\n"+(endTime-startTime)+"ms"+"\n");
System.out.println("\n"+"---------------------------------");
System.out.println("冒泡排序法对数组进行排序:"+"\n");
long startTime2=System.currentTimeMillis();
for (int i = 0; i < a.length; i++) {
for (int j = i+1; j < a.length; j++) {
if(a[i]>a[j]){
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
System.out.print(a[i]+",");
}
System.out.println("\n");
long endTime2=System.currentTimeMillis();
System.out.println("冒泡排序法所用的时间为:"+(endTime2-startTime2)+"ms");//用sbuffer来加载
System.out.println("\n"+"---------------------------------");
System.out.println("插入排序法:"+"\n");
long startTime3=System.currentTimeMillis();
for (int i = 0; i < a.length; i++) {
for(int j=i ;j>0;j--){
if(a[j]>a[i]){
temp=a[j-1];
a[j-1]=a[j];
a[j]=temp;
}
else
break;
}
}
for (Integer b : a) {
System.out.print(b + ",");
}
long endTime3=System.currentTimeMillis();
System.out.println("\n");
System.out.print("\n"+"用插入排序法所用的时间为:"+(endTime3-startTime3)+"ms");
Arrays.sort(a);
System.out.println("\n"+"-------------------");
System.out.println("快速排序法:");
System.out.println("\n");
long startTime4=System.currentTimeMillis();
for (int i = 0; i < a.length; i++) {
System.out.print(a[i]+",");
}
long endTime4=System.currentTimeMillis();
System.out.println("\n");
System.out.println("\n"+"快速排序法所用时间:"+(endTime4-startTime4)+"ms");
}
}