Soluzione 6.6

Ecco una possibile classe di collaudo ArrayAlgsTester. Abbiamo usato il metodo System.currentTimeMillis() per la rilevazione delle prestazioni dei metodi.
import java.util.Scanner;

public class ArrayAlgsTester
{
    public static void main(String[] args)
    {
        int n = 0;
        int m = 0;
        int[] a = null;
        int aSize = 0;

        Scanner in = new Scanner(System.in);
        while (n<=0 && m<=0)
        {
            try
            {
                System.out.println("Inserire un int > 0 (lunghezza array)"); 
                n = Integer.parseInt(in.nextLine());
                System.out.println("Inserire un int > 0 (max valore in array)");
                m = Integer.parseInt(in.nextLine());
            }
            catch(NumberFormatException e)
            { }
        }

        // ------ collaudo algoritmi di ordinamento -----
        boolean done = false;
        while (!done)
        {
            System.out.println("Ordinamento (S=selectionSort, I=insertionSort"); 
            System.out.println("             M=mergeSort, Q=quit)"); 

            String cmd;
            if ((cmd=in.nextLine()).equalsIgnoreCase("Q"))
                done = true;
            else
            {  a = ArrayAlgs.randomIntArray(n,m);
               aSize = a.length;
               long time = 0;
                if (cmd.equalsIgnoreCase("S"))
                {   time = System.currentTimeMillis();
                    ArrayAlgs.selectionSort(a,aSize);
                    time = System.currentTimeMillis() - time;
                    System.out.printf("SelectionSort: %.3f s%n",time/1000.0);
                }
                else if (cmd.equalsIgnoreCase("I"))
                {   time = System.currentTimeMillis();
                    ArrayAlgs.insertionSort(a,aSize);
                    time = System.currentTimeMillis() - time;
                    System.out.printf("InsertionSort: %.3f s%n",time/1000.0);
                }
                else if (cmd.equalsIgnoreCase("M"))
                {   time = System.currentTimeMillis();
                    ArrayAlgs.mergeSort(a,aSize);
                    time = System.currentTimeMillis() - time;
                    System.out.printf("MergeSort: %.3f s%n",time/1000.0);
                }
            }
        }

        // ------ collaudo algoritmi di ricerca -----
        done = false;
        while (!done)
        {
            int value = -1;            
            System.out.println("Valore int>0 da cercare (Q per terminare)? ");
            String cmd=null;
            try{ value = Integer.parseInt(cmd=in.nextLine()); }
            catch(NumberFormatException e)
            { if (cmd.equalsIgnoreCase("Q")) done = true; }
            if (value > 0)
            {
                System.out.println("Ricerca (L=linearSearch, B=binSearch)"); 
                long time = 0;
                int i = 0;
                cmd = in.nextLine();
                if (cmd.equalsIgnoreCase("L"))
                {   time = System.currentTimeMillis();
                    i = ArrayAlgs.linearSearch(a,aSize,value);
                    time = System.currentTimeMillis() - time;
                    if (i>=0) System.out.println("Trovato in a[" +i+ "]");
                    else System.out.println("Non trovato");
                    System.out.printf("LinearSearch: %.3f s%n",time/1000.0);
                }
                else if (cmd.equalsIgnoreCase("B"))
                {   time = System.currentTimeMillis();
                    i = ArrayAlgs.binarySearch(a,aSize,value);
                    time = System.currentTimeMillis() - time;
                    if (i>=0) System.out.println("Trovato in a[" +i+ "]");
                    else System.out.println("Non trovato");
                    System.out.printf("BinarySearch: %.3f s%n",time/1000.0);
                }
            }
        }
    }
}