Exemplo n.º 1
0
  public static void run(int[] array) {
    if (array == null || array.length < 2) return;

    SelectionSort ss = new SelectionSort();
    ss.array = array;
    ss.sort();
  }
 /** Test InsertionIndex.sort() on given array */
 public void testSelectionSort(int[] arr) {
   System.out.println(String.format("Selection sort on input: %s", Arrays.toString(arr)));
   SelectionSort.sort(arr);
   System.out.println(String.format("Result: %s", Arrays.toString(arr)));
   System.out.println();
   assertTrue(TestUtil.isArraySorted(arr));
 }
Exemplo n.º 3
0
  public static void main(String[] args) {
    //  create object to perform selection sort
    SelectionSort sortArray = new SelectionSort(10);

    System.out.println("Unsorted array:");
    System.out.println(sortArray + "\n"); //  print unsorted array.

    sortArray.sort();

    System.out.println("Sorted array:");
    System.out.println(sortArray);
  }
Exemplo n.º 4
0
  public static void main(String[] args) {
    // Array length
    int length = 20000;
    // Random number range
    int range = 200;
    // Print results
    boolean print = false;
    // For measuring performance
    long startTime, endTime;

    int[] unsortedArray = new int[length];

    // Array of randomly generated numbers
    Random generator = new Random();
    for (int i = 0; i < unsortedArray.length; i++) {
      unsortedArray[i] = generator.nextInt(range);
    }

    // BubbleSort run
    startTime = System.nanoTime();
    int[] bubbleSortedArray = BubbleSort.sort(unsortedArray);
    endTime = System.nanoTime();
    System.out.println("BubbleSort " + (endTime - startTime));

    // SelectionSort run
    startTime = System.nanoTime();
    int[] selectionSortedArray = SelectionSort.sort(unsortedArray);
    endTime = System.nanoTime();
    System.out.println("SelectionSort " + (endTime - startTime));

    // HeapSort run
    startTime = System.nanoTime();
    int[] heapSortedArray = HeapSort.sort(unsortedArray);
    endTime = System.nanoTime();
    System.out.println("HeapSort " + (endTime - startTime));

    // Print the arrays
    if (print) {
      Helper.printArray(unsortedArray);
      Helper.printArray(bubbleSortedArray);
      Helper.printArray(heapSortedArray);
      Helper.printArray(selectionSortedArray);
    }
  }
Exemplo n.º 5
0
  /** @param args the command line arguments */
  public static void main(String[] args) {
    SelectionSort sorter = new SelectionSortImpl();
    long gesammtvergleiche = 0;
    int gesammtmillisec = 0;
    int n = 10;
    int durchlaufanz = 100;
    Random rand = new Random();
    for (int durchlaeufe = 0; durchlaeufe < durchlaufanz; durchlaeufe++) {
      int[] tests = new int[n];
      for (int i = 0; i < tests.length; i++) {
        tests[i] = rand.nextInt();
      }
      long time1 = System.currentTimeMillis();
      tests = sorter.sort(tests);
      long time2 = System.currentTimeMillis() - time1;

      gesammtvergleiche += sorter.getAnzVergleiche();
      gesammtmillisec += time2;
    }
    System.out.println("Gesammt: " + gesammtmillisec / durchlaufanz + " ms");
    System.out.println(gesammtvergleiche / durchlaufanz + " Vergleiche");
  }
Exemplo n.º 6
0
 @Test
 public void testSelectionSort() {
   List<Integer> list = getRandomUnsortedList();
   testSort(list, SelectionSort.sort(list));
 }