📄 gettendency.java
字号:
import javax.swing.JOptionPane;
public class GetTendency
{
public static void main(String[] args)
{
//Input the length of the array.
String strLengthOfArray = JOptionPane.showInputDialog(null, "Enter the length of the array:", "Input", JOptionPane.QUESTION_MESSAGE);
int lengthOfArray = 0;
try
{
lengthOfArray = Integer.parseInt(strLengthOfArray);
}
catch (Exception e)
{
JOptionPane.showMessageDialog(null, "Please enter an integer!", "Error", JOptionPane.WARNING_MESSAGE);
System.exit(0);
}
long timeSpentByInsertionSort = 0;
long timeSpentByQuickSort = 0;
//Do the experiments one million times in order to reduce the coincidence.
for (int k = 1; k <= 1000000; k ++)
{
//Create the sorce arrays.
double[] sourceArrayForInsertionSort = new double[lengthOfArray];
double[] sourceArrayForQuickSort = new double[lengthOfArray];
for (int n = 0; n < sourceArrayForInsertionSort.length; n ++)
sourceArrayForInsertionSort[n] = Math.random();
for (int n = 0; n < sourceArrayForQuickSort.length; n ++)
sourceArrayForQuickSort[n] = sourceArrayForInsertionSort[n];
//Register the time spent by insertion sort.
Sort insertionSort = new Sort(sourceArrayForInsertionSort);
long startTimeOfInsertionSort = System.nanoTime();
insertionSort.insertionSort(0, sourceArrayForInsertionSort.length - 1);
timeSpentByInsertionSort += System.nanoTime() - startTimeOfInsertionSort;
//Register the time spent by quick sort.
Sort quickSort = new Sort(sourceArrayForQuickSort);
long startTimeOfQuickSort = System.nanoTime();
quickSort.quickSort(0, sourceArrayForQuickSort.length - 1);
timeSpentByQuickSort += System.nanoTime() - startTimeOfQuickSort;
}
//Output the result.
System.out.println("The length of the array is: " + lengthOfArray);
System.out.println("The average nanoseconds spent by insertion sort is: " + (timeSpentByInsertionSort / 1000000));
System.out.println("The average nanoseconds spent by quick sort is: " + (timeSpentByQuickSort / 1000000));
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -