// CompareSortRoutines.java // ปรับปรุง : 2548-05-26 () // Comparison of simple sorting algorithms class CompareSortRoutines { public static void main(String[] args) { //array of 10000 objects System.out.println("Slow because 10000 data sample."); DynamicArray a = new DynamicArray(); DynamicArray b = new DynamicArray(); DynamicArray c = new DynamicArray(); long start, stop, totalstart, totalstop; double timeUsed; //assign random numbers between 1 and 10000 for(int i = 0; i < 10000; i++) { int n = (int)(Math.random() * 100) + 1; a.insert(new Integer(n)); b.insert(new Integer(n)); c.insert(new Integer(n)); } totalstart = System.currentTimeMillis(); //calculate time used for Bubble sort start = System.currentTimeMillis(); a.bubbleSort(); stop = System.currentTimeMillis(); timeUsed = (double)(stop - start) / 1000.0D; System.out.println("Bubble sort time used: " + timeUsed + " seconds."); //calculate time used for Selection sort start = System.currentTimeMillis(); b.selectionSort(); stop = System.currentTimeMillis(); timeUsed = (double)(stop - start) / 1000.0D; System.out.println("Selection sort time used: " + timeUsed + " seconds."); //calculate time used for Insertion sort start = System.currentTimeMillis(); c.insertionSort(); stop = System.currentTimeMillis(); timeUsed = (double)(stop - start) / 1000.0D; System.out.println("Insertion sort time used: " + timeUsed + " seconds."); //calculate total time totalstop = System.currentTimeMillis(); timeUsed = (double)(totalstop - totalstart) / 1000.0D; System.out.println(" =========="); System.out.println("Total time used: " + timeUsed + " seconds."); } }