i m working with this program but dont know how to add counter in it in order to check running / execution time of program.please help me. and telll how and where to add code for this task?plz reply soon.

public class InsertionSort{
    public static void main(String a[]){
        int i;
        int array[] = {12,9,4,99,120,1,3,10};

        System.out.println("       Insertion Sort\n\n");   
        System.out.println("Values Before the sort:\n");    
        for(i = 0; i < array.length; i++)
        System.out.print( array[i]+"  ");
        System.out.println();
        insertion_srt(array, array.length);        
        System.out.print("Values after the sort:\n");    
        for(i = 0; i <array.length; i++)
        System.out.print(array[i]+"  ");
        System.out.println();   
        System.out.println("PAUSE");                 
    }

    public static void insertion_srt(int array[], int n){
        for (int i = 1; i < n; i++){
            int j = i;
            int B = array[i];
            while ((j > 0) && (array[j-1] > B)){
                array[j] = array[j-1];
                j--;
            }
            array[j] = B;
        }
    }
}

Edited 3 Years Ago by mike_2000_17: Fixed formatting

System.currentTimeMillis() will give you the current time in milliseconds. Call this method before your code does anything else, then again after your code is finished, then find the difference and you have the number of milliseconds that your code ran for.

This article has been dead for over six months. Start a new discussion instead.