Java 测量的插入排序速度太快
我正在用Java快速测试各种排序算法,通过向它们提供随机生成的数字数组,我得到了奇怪的插入排序结果 我使用System.nanoTime()来度量运行时,插入排序的值低于快速排序和合并排序,即使在排序大型数组时也是如此,这似乎是错误的。我在程序中看到气泡排序或选择排序的正常(缓慢)运行时间,因此我认为我没有正确地测量insertionSort的速度,但我不确定如何:Java 测量的插入排序速度太快,java,algorithm,sorting,insertion-sort,Java,Algorithm,Sorting,Insertion Sort,我正在用Java快速测试各种排序算法,通过向它们提供随机生成的数字数组,我得到了奇怪的插入排序结果 我使用System.nanoTime()来度量运行时,插入排序的值低于快速排序和合并排序,即使在排序大型数组时也是如此,这似乎是错误的。我在程序中看到气泡排序或选择排序的正常(缓慢)运行时间,因此我认为我没有正确地测量insertionSort的速度,但我不确定如何: public static int[] insertionSort(int[] array) { long startTi
public static int[] insertionSort(int[] array) {
long startTime = System.nanoTime();
int current;
int innerIdx;
int arrayLength = array.length;
for (int idx = 1; idx < arrayLength; idx++) {
// iterate through the array, happens just once
current = array[idx];
for (innerIdx = idx - 1; current < array[innerIdx] && innerIdx >= 0; innerIdx--) {
// while current is "traveling" over larger values in array
array[innerIdx + 1] = array[innerIdx];
// shift the array elements over by 1 index
}
array[innerIdx + 1] = current;
}
long endTime = System.nanoTime() - startTime;
System.out.println("Insertion Sort runtime (ns) = " + endTime);
return array;
}
公共静态int[]插入排序(int[]数组){
long startTime=System.nanoTime();
电流;
intinnerIDX;
int arrayLength=array.length;
对于(int-idx=1;idx=0;innerIdx--){
//当电流在阵列中的较大值上“移动”时
数组[innerIdx+1]=数组[innerIdx];
//将数组元素移动1个索引
}
数组[innerIdx+1]=当前;
}
long endTime=System.nanoTime()-startTime;
System.out.println(“插入排序运行时(ns)=”+endTime);
返回数组;
}
以下是主要方法,其中包含气泡和选择,它们似乎可以正确测量(merge和quick在单独的类中):
import java.util.Random;
导入java.util.Scanner;
公共类算法{
静态布尔显示输出=false;
公共静态void main(字符串[]args){
//TODO自动生成的方法存根
扫描仪=新的扫描仪(System.in);
System.out.println(“您想要测试数组多长时间?(给出整数)”;
int lengthOfTestArray=scanner.nextInt();
System.out.println(“生成介于1和…(给定整数)之间的随机值”);
int=scanner.nextInt();
System.out.println(“在屏幕上显示排序的数组?”('true'或'false');
displayOutput=scanner.nextBoolean();
System.out.println(“-~-~-~(╯ಠ_ರೃ)╯︵ ┻━┻ 让我们这样做!\n“;
//结束输入时,用户已定义数组的大小和值的范围
int[]testArray=新int[lengthOfTestArray];
for(int idx=0;idx0;idx--){
if(数组[idx]<数组[idx-1]){
trailingNum=数组[idx];
数组[idx]=数组[idx-1];
数组[idx-1]=trailingNum;
flag=true;
}
}
}
long endTime=System.nanoTime()-startTime;
System.out.println(“气泡排序运行时(ns)=”+endTime);
返回数组;
}
公共静态int[]选择排序(int[]数组){
//发现下一个元素出现错误,用正确的位置交换
//正在上升
long startTime=System.nanoTime();
int arrayLength=array.length;
for(int-idx=0;idximport java.util.Random;
import java.util.Scanner;
public class Algorithms {
static boolean displayOutput = false;
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner = new Scanner(System.in);
System.out.println("How long do you want the test array? (give integer)");
int lengthOfTestArray = scanner.nextInt();
System.out.println("Generate random values between 1 and... (give integer)");
int randomCeiling = scanner.nextInt();
System.out.println("Display sorted arrays onto screen? ('true' or 'false')");
displayOutput = scanner.nextBoolean();
System.out.println("-~-~-~(╯ಠ_ರೃ)╯︵ ┻━┻ LET'S DO THIS! \n");
// END INPUT, user has defined size of array and range for values
int[] testArray = new int[lengthOfTestArray];
for (int idx = 0; idx < testArray.length; idx++) {
// generate an array of random numbers
Random rand = new Random();
testArray[idx] = rand.nextInt(randomCeiling) + 1;
}
// create copies for each of the sorting algorithms
int[] bubbleArray = testArray;
int[] selectionArray = testArray;
int[] insertionArray = testArray;
int[] quickArray = testArray;
int[] mergeArray = testArray;
bubbleArray = bubbleSort(bubbleArray);
System.out.println("Bubble Sort:");
for (int value : bubbleArray) {
System.out.print(value + " ");
}
System.out.println("\n-----------------");
selectionArray = selectionSort(selectionArray);
System.out.println("Selection Sort Output:");
if (displayOutput) {
for (int value : selectionArray) {
System.out.print(value + " ");
}
}
System.out.println("\n-----------------");
insertionArray = insertionSort(insertionArray);
System.out.println("Insertion Sort Output");
if (displayOutput) {
for (int value : insertionArray) {
System.out.print(value + " ");
}
}
System.out.println("\n-----------------");
Quicksort.run(quickArray);
System.out.println("Quick Sort Output");
if (displayOutput) {
for (int value : quickArray) {
System.out.print(value + " ");
}
}
System.out.println("\n-----------------");
Mergesort.run(quickArray);
System.out.println("Merge Sort Output");
if (displayOutput) {
for (int value : mergeArray) {
System.out.print(value + " ");
}
}
}
public static int[] bubbleSort(int[] array) {
// bubble sort iterates through array, rearranging 2 at a time
// currently makes it ASCENDING
long startTime = System.nanoTime();
int trailingNum = 0;
int arrayLength = array.length;
boolean flag = true;
while (flag) {
flag = false;
for (int idx = arrayLength - 1; idx > 0; idx--) {
if (array[idx] < array[idx - 1]) {
trailingNum = array[idx];
array[idx] = array[idx - 1];
array[idx - 1] = trailingNum;
flag = true;
}
}
}
long endTime = System.nanoTime() - startTime;
System.out.println("Bubble Sort runtime (ns) = " + endTime);
return array;
}
public static int[] selectionSort(int[] array) {
// finds next element out of order, swaps with correct position
// currently ASCENDING
long startTime = System.nanoTime();
int arrayLength = array.length;
for (int idx = 0; idx < array.length; idx++) {
int lastMinimum = 0;
int lastMinimumIndex = 0;
for (int innerIdx = idx; innerIdx < array.length; innerIdx++) {
if (innerIdx == idx) {
// if first iteration, set the minimum as first value
lastMinimumIndex = innerIdx;
}
if (array[innerIdx] < array[lastMinimumIndex]) {
// if value at position smaller than min index, replace
lastMinimumIndex = innerIdx;
}
// loop exit with best min index starting from index of outer
}
if (idx != lastMinimumIndex) {
// if minimum value is not at the current index
int tempMin = array[lastMinimumIndex];
array[lastMinimumIndex] = array[idx];
array[idx] = tempMin;
}
}
long endTime = System.nanoTime() - startTime;
System.out.println("Selection Sort runtime (ns) = " + endTime);
return array;
}
public static int[] insertionSort(int[] array) {
long startTime = System.nanoTime();
int current;
int innerIdx;
int arrayLength = array.length;
for (int idx = 1; idx < arrayLength; idx++) {
// iterate through the array, happens just once
current = array[idx];
for (innerIdx = idx - 1; current < array[innerIdx] && innerIdx >= 0; innerIdx--) {
// while current is "traveling" over larger values in array
array[innerIdx + 1] = array[innerIdx];
// shift the array elements over by 1 index
}
array[innerIdx + 1] = current;
}
long endTime = System.nanoTime() - startTime;
System.out.println("Insertion Sort runtime (ns) = " + endTime);
return array;
}
// create copies for each of the sorting algorithms
int[] bubbleArray = testArray;
int[] selectionArray = testArray;
int[] insertionArray = testArray;
int[] quickArray = testArray;
int[] mergeArray = testArray;