Ejemplo n.º 1
0
 public static double GetAverageConstantTime(algorithm a) {
   int arrayLength = 10000;
   int totalTime = 0;
   for (int i = 0; i < 5; i++) {
     int[] arr = getRandomArray(arrayLength, 100);
     long bt = System.currentTimeMillis();
     if (a == algorithm.Insertion) {
       InsertionSorter.Sort(arr);
     } else if (a == algorithm.Quick) {
       QuickSorter.Sort(arr);
     } else if (a == algorithm.Merge) {
       MergeSorter.Sort(arr);
     } else if (a == algorithm.Radix) {
       RadixSorter.Sort(arr);
     }
     totalTime += System.currentTimeMillis() - bt;
   }
   double averageTime = (double) totalTime / 5;
   if (a == algorithm.Insertion) {
     return averageTime / (arrayLength * arrayLength);
   } else if (a == algorithm.Quick) {
     return averageTime / (arrayLength * (Math.log(arrayLength) / Math.log(2)));
   } else if (a == algorithm.Merge) {
     return averageTime / (Math.log(arrayLength) / Math.log(2));
   } else if (a == algorithm.Radix) {
     return averageTime / arrayLength;
   }
   return 0;
 }
Ejemplo n.º 2
0
  public static void Test() {
    while (true) {
      System.out.println(
          "Test of sorting algorithms. Choose an algorithm to test by entering it's number.");
      System.out.println("1: Insertion sort");
      System.out.println("2: Quicksort");
      System.out.println("3: Merge sort");
      System.out.println("4: Radix sort");

      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("Algorithm number:");
      int selectedAlgorithm = 0;
      try {
        selectedAlgorithm = Integer.parseInt(reader.readLine());
      } catch (Exception e) {
        e.printStackTrace();
      }
      System.out.print("Enter a length for the array to be sorted:");
      int selectedLength = 0;
      try {
        selectedLength = Integer.parseInt(reader.readLine());
      } catch (Exception e) {
        e.printStackTrace();
      }
      System.out.print("Enter a size range for the numbers. 0 to :");
      int selectedSize = 0;
      try {
        selectedSize = Integer.parseInt(reader.readLine());
      } catch (Exception e) {
        e.printStackTrace();
      }

      int[] randArr = getRandomArray(selectedLength, selectedSize);

      long beforeTime = System.currentTimeMillis();
      switch (selectedAlgorithm) {
        case 1:
          InsertionSorter.Sort(randArr);
          long timeI = System.currentTimeMillis() - beforeTime;
          System.out.println("INSERTION SORT: " + timeI + "ms");
          System.out.printf("C = %.10fms\n", GetAverageConstantTime(algorithm.Insertion));
          break;
        case 2:
          QuickSorter.Sort(randArr);
          long time = System.currentTimeMillis() - beforeTime;
          System.out.println("QUICKSORT: " + time + "ms");
          System.out.printf("C = %.10fms\n", GetAverageConstantTime(algorithm.Quick));
          break;
        case 3:
          MergeSorter.Sort(randArr);
          long timeM = System.currentTimeMillis() - beforeTime;
          System.out.println("MERGE SORT: " + timeM + "ms");
          System.out.printf("C = %.10fms\n", GetAverageConstantTime(algorithm.Merge));
          break;
        case 4:
          RadixSorter.Sort(randArr);
          long timeR = System.currentTimeMillis() - beforeTime;
          System.out.println("RADIX SORT: " + timeR + "ms");
          System.out.printf("C = %.10fms\n", GetAverageConstantTime(algorithm.Radix));
          break;
        default:
          System.out.println("Not a valid algorithm");
      }
    }
  }