该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
import java.util.Date;
import java.text.SimpleDateFormat;
class BubbleSortApp {
public static void main(String[] args) {
System.out.println("=======冒泡排序法=======");
for(int i = 1;i<=10;i++){
int maxSize = 10000*i; // array size
ArrayBub arr; // reference to array
arr = new ArrayBub(maxSize); // create the array
// for(int j = maxSize-1;j>=0;j--)
// {
// arr.insert(j);
// }
for(int j=0; j
{ // random numbers
long n = (long)( java.lang.Math.random()*(maxSize-1) );
arr.insert(n);
}
SimpleDateFormat df = new SimpleDateFormat("HH:mm:ss:SSS");//设置日期格式
Date before = new Date();
System.out.println(maxSize+"个数:before sorted:"+df.format(before) + " "+"Sorting...");// new Date()为获取当前系统时间
arr.bubbleSort(); // bubble sort them
Date after = new Date();
System.out.print(" after sorted:"+df.format(after));
long interval=after.getTime()-before.getTime();// 计算时间间隔,毫秒单位
System.out.println(" 排序用时:"+interval);
// System.out.println();
} // end main()
} // end class BubbleSortApp
}
class ArrayBub
{
private long[] a; // ref to array a
private int nElems; // number of data items
int sort1 = 0,sort2 = 0;
//--------------------------------------------------------------
public ArrayBub(int max) // constructor
{
a = new long[max]; // create the array
nElems = 0; // no items yet
}
//--------------------------------------------------------------
public void insert(long value) // put element into array
{
a[nElems] = value; // insert it
nElems++; // increment size
}
//--------------------------------------------------------------
public void display() // displays array contents
{
for(int j=0; j
System.out.print(a[j] + " "); // display it
System.out.println("");
}
//--------------------------------------------------------------
public void bubbleSort()
{
int out, in;
for(out=nElems-1; out>=1; out--) // outer loop (backward)
for(in=0; in
if( a[in] > a[in+1] ) // out of order?
swap(in, in+1); // swap them
} // end bubbleSort()
//--------------------------------------------------------------
private void swap(int one, int two)
{
long temp = a[one];
a[one] = a[two];
a[two] = temp;
}
//--------------------------------------------------------------
} // end class ArrayBub