Esempio n. 1
0
 private static void Sort(Comparable[] arr) {
   int N = arr.length;
   int h = 1;
   while (h < N / 3) h = 3 * h + 1;
   while (h >= 1) {
     for (int i = h; i < N; i++) {
       for (int j = i; j >= h && SortUtil.less(arr[j], arr[j - h]); j -= h) {
         SortUtil.exch(arr, j, j - h);
       }
     }
     h = h / 3;
   }
 }
Esempio n. 2
0
  /**
   * Merges the two subparts
   *
   * @param input The input
   * @param aux The auxiliary array
   * @param low Indicates the starting index
   * @param mid Indicates the mid index
   * @param high Indicates the ending index
   */
  private static int mergeAndCount(
      Comparable[] input, Comparable[] aux, int low, int mid, int high) {
    int i = low;
    int j = mid + 1;
    int inversions = 0;
    // copy items to auxiliary array
    for (int k = low; k <= high; k++) {
      aux[k] = input[k];
    }

    for (int k = low; k <= high; k++) {
      if (i > mid) {
        input[k] = aux[j];
        j++;
      } else if (j > high) {
        input[k] = aux[i];
        i++;
      } else if (SortUtil.greaterThan(aux[i], aux[j])) {
        input[k] = aux[j];
        j++;
        inversions += mid + 1 - i;
      } else {
        input[k] = aux[i];
        i++;
      }
    }
    return inversions;
  }
Esempio n. 3
0
 /**
  * (non-Javadoc)
  *
  * @see cn.benworks.utils.algorithm.sort.array.SortUtil.Sort#sort(int[])
  */
 public void sort(int[] data) {
   // int temp;
   for (int i = 0; i < data.length; i++) {
     int lowIndex = i;
     for (int j = data.length - 1; j > i; j--) {
       if (data[j] < data[lowIndex]) {
         lowIndex = j;
       }
     }
     SortUtil.swap(data, i, lowIndex);
   }
 }
Esempio n. 4
0
 private static int sortAndCount(Comparable[] input, Comparable[] aux, int low, int high) {
   if (high <= low) {
     // base case
     return 0;
   }
   int inversions = 0;
   int mid = ((high - low) / 2) + low;
   inversions += sortAndCount(input, aux, low, mid);
   inversions += sortAndCount(input, aux, mid + 1, high);
   // prevent merging if both subparts are sorted
   if (SortUtil.greaterThan(input[mid], input[mid + 1])) {
     inversions += mergeAndCount(input, aux, low, mid, high);
   }
   return inversions;
 }
Esempio n. 5
0
 public static void main(String[] args) {
   Comparable[] data = {
     4, 6, 5, 64, 8, 9, 15, 3, 56, 768, 5679, 87, 9, 89, 30, 23, 12, 38, 86, 90, 45, 23, 78, 32,
     43, 54, 65, 76, 876, 98, 5768, 435, 245, 647, 678, 987, 2345, 2, 34556, 5, 234, 56785, 46464,
     6423, 45643, 4578, 6564, 2234, 3456756, 43, 4, 8, 78689, 190, 6575, 89, 4, 6, 5, 64, 8, 9, 15,
     3, 56, 768, 5679, 87, 9, 89, 30, 23, 12, 38, 86, 90, 45, 23, 78, 32, 43, 54, 65, 76, 876, 98,
     5768, 435, 245, 647, 678, 987, 2345, 2, 34556, 5, 234, 56785, 46464, 6423, 45643, 4578, 6564,
     2234, 3456756, 43, 4, 8, 78689, 190, 6575, 89, 4, 6, 5, 64, 8, 9, 15, 3, 56, 768, 5679, 87, 9,
     89, 30, 23, 12, 38, 86, 90, 45, 23, 78, 32, 43, 54, 65, 76, 876, 98, 5768, 435, 245, 647, 678,
     987, 2345, 2, 34556, 5, 234, 56785, 46464, 6423, 45643, 4578, 6564, 2234, 3456756, 43, 4, 8,
     78689, 190, 6575, 89, 4, 6, 5, 64, 8, 9, 15, 3, 56, 768, 5679, 87, 9, 89, 30, 23, 12, 38, 86,
     90, 45, 23, 78, 32, 43, 54, 65, 76, 876, 98, 5768, 435, 245, 647, 678, 987, 2345, 2, 34556, 5,
     234, 56785, 46464, 6423, 45643, 4578, 6564, 2234, 3456756, 43, 4, 8, 78689, 190, 6575, 89
   };
   //		Comparable []data =
   // {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1};
   try {
     SortUtil.print(data);
     Comparable[] c = data.clone();
     Exchange_BubbleSort.sort(c);
     SortUtil.print(c);
     c = data.clone();
     Exchange_CocktailSort.sort(c);
     SortUtil.print(c);
     c = data.clone();
     Selection_SelectionSort.sort(c);
     SortUtil.print(c);
     c = data.clone();
     Insertion_InsertionSort.sort(c);
     SortUtil.print(c);
     c = data.clone();
     Merge_MergeSort.sort(c);
     SortUtil.print(c);
     c = data.clone();
     Exchange_QuickSort.sort(c);
     SortUtil.print(c);
     c = data.clone();
     Selection_HeapSort.sort(c);
     SortUtil.print(c);
   } catch (Exception e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
   }
 }
Esempio n. 6
0
 public static void main(String[] args) {
   Sort(arr);
   SortUtil.show(arr);
 }
 public static void main(String[] args) {
   int[] input = SortUtil.getRandomArrayOfInts();
   new InsertionSort().sort(input);
 }
 /** @param args */
 public static void main(String[] args) {
   int[] data = SortUtil.data;
   insertionSort(data);
   SortUtil.printArray(data);
 }