选择排序计时器增加时间而不是经过的时间

时间:2017-03-30 01:34:16

标签: java sorting select

我是计时选择排序,但计时器显示增量时间而不是运行时间。它以纳秒为单位给出当前计数的快照。有人能告诉我为什么我的时机正在这样做。 例: 3741356 3765080 3806116 3850358 3869594 3917684 3940125 3979879 4055540

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

public class SelectionSortFinal
{
   public static void main(String[] args) {
      Random rand = new Random();
      Scanner input = new Scanner(System.in);

  int m;
  int n = 0;
  int num_i = 0;
  long startTime = System.nanoTime();  
  long running_time;
  System.out.print("Input array size: ");
  n = input.nextInt();
  System.out.print("Input number of arrays: ");
  num_i = input.nextInt();


  for(int j =  0; j < num_i; j++){

  }
  int array[] = new int [n];
  {

  }
  for(int i = 0; i < n; i++)
  {
      for(m = 0; m < array.length; m++){


           array[m] = (int)(Math.random()*100);

  }
     int temp = 0;
     int min;
     int[] sorted = new int[n];
     for (int k = 0; k < array.length; k++) {
        for (int j = 1; j < (array.length - k); j++) {
         // System.out.println("Start Time: " + startTime);
              startTime = System.nanoTime();

          for (j = 0; j < array.length-1; j++) {

              min = j;

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

              if ( min != j ) {
                      temp = array[j];
                      array[j] = array[min];
                      array[min] = temp;

      }
           long endTime = System.nanoTime();
           running_time = endTime - startTime;
          // System.out.println("End Time: " + endTime);
           System.out.println(running_time);
        }


     }

      }

   }
   }
}

0 个答案:

没有答案