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[10000];
long startTime=System.currentTimeMillis();
for (int i = 0; i < a.length; i++) {
a[i]=random.nextInt(10000);
}
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[j-1]){
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");
}
}
网友评论