Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Sort - Main.java

Main.java

Caricato da:
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package sort;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.IOException;
  9. import java.io.InputStreamReader;
  10. import java.util.Random;
  11.  
  12. /**
  13.  *
  14.  * @author Paolo
  15.  */
  16. public class Main {
  17.  
  18.     /**
  19.      * @param args the command line arguments
  20.      */
  21.     public static void main(String[] args) throws IOException {
  22.         BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
  23.         String sortMethod = "";
  24.         Integer length = 0;
  25.         Character insert = 'y';
  26.         Random rand = new Random();
  27.         System.out.print("Give me the sort method (merge/bubble/insert/gnome/quick): ");
  28.         sortMethod = read.readLine();
  29.         if (!SortMethod.validateMethod(sortMethod)) {
  30.             sortMethod = SortMethod.MERGE;
  31.         }
  32.         System.out.print("Give me the array size: ");
  33.         length = Integer.parseInt(read.readLine());
  34.         Integer[] array = new Integer[length];
  35.         System.out.print("Do you want insert random number in array? y/n ");
  36.         insert = read.readLine().charAt(0);
  37.         if (insert != 'y') {
  38.             for (int i = 0; i < length; i++) {
  39.                 System.out.print("Give me the " + (i + 1) + " ° number: ");
  40.                 array[i] = Integer.parseInt(read.readLine());
  41.             }
  42.         } else {
  43.             for (int i = 0; i < length; i++) {
  44.                 array[i] = rand.nextInt(length);
  45.             }
  46.  
  47.         }
  48.         System.out.print("Unsorted array: ");
  49.         for (int i = 0; i < length; i++) {
  50.             System.out.print(array[i] + " ");
  51.         }
  52.         System.out.println();
  53.         long start = System.currentTimeMillis();
  54.         SortMethod met = SortMethod.forName(sortMethod);
  55.         array = met.sort(array);
  56.         long end = System.currentTimeMillis();
  57.         System.out.print("Sorted array: ");
  58.         for (int i = 0; i < array.length; i++) {
  59.             System.out.print(array[i] + " ");
  60.         }
  61.         System.out.println();
  62.         System.out.println("Sort endend in: " + (end - start) + " millisec");
  63.     }
  64. }