Search code examples
javaarraysbenchmarkingselection-sort

How to implement a benchmarking method for selection sort?


I have tried to measure the execution time for selection sorting an array in my code. The selection sort part is working but the benchmarking method "time" does not even get executed. What is wrong with my "time" method and how do I get it to execute and measure performance time?

     public class Selection_Sort
{
        public static void main(String[] args)
        {
            // define an array
            int[] array = {12, 3, 7, 6, 2};
            int i;
            int j;
            int t;
            int smallestNumber;


            // run an outer loop i from 0 to array.length-1 to repeat the process of selection sort
            for(i = 0; i < array.length-1; i++)
            {
                // smallest number position
                smallestNumber = i;

                // run an inner loop j for selection sort from i+1 to array.length
                for(j = i + 1; j < array.length; j++)
                {
                    // now check if the value at array[j] is smaller than value at array[smallestNumber]
                    if(array[j] < array[smallestNumber])
                    {
                        // if the value is smaller, then store the value of j to smallestNumber
                        smallestNumber = j;
                    }
                }

                // outside the body of inner loop j check if array[i] > array[smallestNumber]. If yes then swap the numbers
                if(array[i] > array[smallestNumber])
                {
                    t = array[i];
                    array[i] = array[smallestNumber];
                    array[smallestNumber] = t;
                }
            }

            // print the sorted array
            System.out.print("Selection Sort:\n");
            for(i = 0; i < array.length; i++)
            {
                System.out.print(array[i]+" ");
            }
        }

    public static void time(String[] args)
    {
        /* … The code being measured starts … */
        long startTime = System.nanoTime();

        /* … The code being measured ends … */

        long endTime = System.nanoTime();

        // get the difference between the two nano time valuess
        long timeElapsed = endTime - startTime;

        System.out.println("Execution time in nanoseconds: " + timeElapsed);
        System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
    }
}

Solution

  • You need to have the code you want to measure in between the start time and end time. One way to do it is to place the code portion you want to measure in a different method then call it like this:

    public class Selection_Sort{
    public static void sortArray(int[] array)
    {
    
        int smallestNumber;
        int t;
    
        // run an outer loop i from 0 to array.length-1 to repeat the process of selection sort
        for(int i = 0; i < array.length-1; i++)
        {
            // smallest number position
            smallestNumber = i;
    
            // run an inner loop j for selection sort from i+1 to array.length
            for(int j = i + 1; j < array.length; j++)
            {
                // now check if the value at array[j] is smaller than value at array[smallestNumber]
                if(array[j] < array[smallestNumber])
                {
                    // if the value is smaller, then store the value of j to smallestNumber
                    smallestNumber = j;
                }
            }
    
            // outside the body of inner loop j check if array[i] > array[smallestNumber]. If yes then swap the numbers
            if(array[i] > array[smallestNumber])
            {
                t = array[i];
                array[i] = array[smallestNumber];
                array[smallestNumber] = t;
            }
        }
    
       
    }
    
      public static void main(String[] args)
      {
          // define an array
          int[] array = {12, 3, 7, 6, 2};
          /* … The code being measured starts … */
          long startTime = System.nanoTime();
    
          
          sortArray( array);
          /* … The code being measured ends … */
    
          long endTime = System.nanoTime();
    
          // get the difference between the two nano time valuess
          long timeElapsed = endTime - startTime;
    
          System.out.println("Execution time in nanoseconds: " + timeElapsed);
          System.out.println("Execution time in milliseconds: " + timeElapsed / 1000000);
          
          // print the sorted array
          System.out.print("Selection Sort:\n");
          for(int i = 0; i < array.length; i++)
          {
              System.out.print(array[i]+" ");
          }
          
      }
          
      }