测量选择排序 for 循环的持续时间

Measuring the duration of a selection sort for-loop

预先致歉,这是我第一次 posting,而且我是编程新手。现在我正在编写一个程序,计算选择性排序算法完成排序所需的时间。该代码需要用户输入数组中有多少整数以及 for 循环在停止之前将经过多少次迭代,但我有 运行 两个错误。

首先,我设置的定时器输出一直为0。 其次,我对如何实现我希望 for 循环到 运行 的次数有点困惑。

我已经成功 运行 我的代码没有错误,包括我认为我应该做的关于设置迭代次数的事情,但是如果我无法在计时器上看到正确的输出,我无法判断如果我是正确的。

这是代码。我一直在为两个输入使用 10,即数组大小和迭代次数,尽管更改这两个数字对我来说还没有产生任何结果。

import java.util.Scanner;
import java.util.Random;
import java.util.Arrays;

public class SelectionTimed
{
   public static void main(String [] args)
   {
  //asks user how many integers in the array
  System.out.println("Please enter the number of integers to be created in the array: ");   
  Scanner scan = new Scanner (System.in);
  int nums = scan.nextInt();//scans for user input

  //asks user how many times to perform for loop
  System.out.println("Please enter how many iterations you would like the algorithm to perform.");
  int turns = scan.nextInt();//scans for user imput

  int limit = 100;//max for integer generator
  int [] array = new int[nums];//makes array, sets index to user input

  Random generator = new Random();

  //fills array with random integers
  for (int i=0; i<nums; i++)
     {
        array[i] = generator.nextInt(limit);
     }      

  //prints off the array before sort
  System.out.println("Before sort:");
  System.out.println(Arrays.toString(array));

  //Begin timer   
  long startTime = System.nanoTime();

  //selection sort algorithm
  int min;
  for (int i = 0; i < turns; i++) 
  {
     min = i;
     for (int j = i + 1; j < array.length; j++)
     {
        if (array[j] < array[min]) 
        {
           min = j;
        }
     }
  if (min != i)
  {
     int temp = array[i];
     array[i] = array[min];
     array[min] = temp;
  }
  }

  //End timer
  long endTime = System.nanoTime();

  long timeDiff = (endTime - startTime);
  long duration = (timeDiff/1000000L);

  //prints array after sort
  System.out.println("After sort:");
  System.out.println(Arrays.toString(array));
  System.out.println("Duration of sort:");
  System.out.println(duration);
  }
  }

现在谈到打印 long 变量时 "duration" 我尝试了与打印数组类似的不同迭代方法

System.out.println(Long.toString(duration);

但这并没有改变输出为零。虽然当我尝试只打印 "timeDiff" 而不除法时,它给了我一个四位数的整数。

它还需要打印迭代次数和排序的项目数,但我稍后会处理这些。

如果 post 的格式有误,我再次深表歉意,但如果大家有任何意见,我将不胜感激。

持续时间可以存储为双精度:

Random random = new Random();
int [] array = new int[100000];

for (int i = 0; i < array.length; i++) {
  array[i] = random.nextInt(1000);
}

//Begin timer
long startTime = System.nanoTime();

for (int i = 0; i < array.length; i++) {
  int min = i;
  for (int j = i + 1; j < array.length; j++) {
    if (array[j] < array[min]) {
      min = j;
    }
  }
  if (min != i) {
    int temp = array[i];
    array[i] = array[min];
    array[min] = temp;
  }
}

//End timer
long endTime = System.nanoTime();

long timeDiff = (endTime - startTime);
double duration = (timeDiff/1000000.0);

System.out.println("Duration of sort (nanoseconds):");
System.out.println(timeDiff);
System.out.println("Duration of sort (milliseconds):");
System.out.printf("%.6f", duration);

Duration of sort (nanoseconds):
7354061110
Duration of sort (milliseconds):
7354.061110