Search code examples
javaarraysfor-loopselection-sortnanotime

Measuring the duration of a selection sort for-loop


Apologies upfront, this is my first time posting, and I'm new to programming. Right now I am writing a program that times how long it takes for a selective sort algorithm to finish sorting. The code takes user input for how many integers are in the array and for how many iterations the for loop will go through before stopping, but i have run into two bugs.

First, the output of the timer I have set in keeps outputting 0. Second, I am a little confused on how to implement how many times i want the for loop to run.

I have managed to run my code without errors, including what I think I should do about setting the number of iterations, but without me being able to see the correct output on the timer, I cannot tell if i am correct.

Here is the code. I have been using 10 for both inputs, being the array size and the number of iterations, though changing both numbers hasn't yielded any results yet for me.

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);
  }
  }

Now when it comes to printing the long variable "duration" i have tried different iterations of what is similar to printing the arrays

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

but that doesnt change the output from being zero. Though when i tried just printing "timeDiff" without dividing, it gives me a four digit integer.

It will also need to print the number of iterations and the number of items sorted but i will work those in later.

Again I apologise if the post is formatted wrong, but I would appreciate any input you all have.


Solution

  • The duration could be stored as a double:

    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