测量选择排序for-loop的持续时间

时间:2015-07-01 02:54:14

标签: java arrays for-loop selection-sort nanotime

道歉,这是我第一次发帖,我是编程的新手。现在我正在编写一个程序,计算选择性排序算法完成排序所需的时间。代码获取用户输入数据中有多少整数以及for循环在停止之前将经历多少次迭代,但我遇到了两个错误。

首先,我设置的定时器的输出保持输出0。 其次,我对如何实现我希望for循环运行多少次感到困惑。

我已经设法运行我的代码没有错误,包括我认为我应该做的关于设置迭代次数,但没有我能够在计时器上看到正确的输出,我不知道我是否正确。< / p>

这是代码。我一直在为两个输入使用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);
  }
  }

现在说到打印长变量“持续时间”我尝试了不同的迭代,类似于打印数组

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

但这并没有改变输出为零。虽然当我尝试打印“timeDiff”而不进行分割时,它给出了一个四位整数。

它还需要打印迭代次数和已排序的项目数,但我将在以后工作。

如果帖子格式错误,我再次道歉,但我很感激您的所有输入。

1 个答案:

答案 0 :(得分:2)

持续时间可以存储为双倍:

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