Shell排序无法对输入进行排序

时间:2017-11-05 07:39:44

标签: algorithm shellsort

我正在尝试使用shell排序对整数字符串进行排序,shell排序的代码是:

public class Shell {
 public static void sort(Comparable[] a) {
  int N = a.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 && less(a[j], a[j - h]); j -= h)
     exch(a, j, j - h);
   }
   h = h / 3;
  }
 }

 private static boolean less(Comparable v, Comparable w) {
  return v.compareTo(w) < 0;
 }

 private static void exch(Comparable[] a, int i, int j) {
  Comparable t = a[i];
  a[i] = a[j];
  a[j] = t;
 }

 private static void show(Comparable[] a) {
  for (int i = 0; i < a.length; i++)
   StdOut.print(a[i] + " ");
  StdOut.println();
 }

 public static boolean isSorted(Comparable[] a) {
  for (int i = 1; i < a.length; i++)
   if (less(a[i], a[i - 1])) return false;
  return true;
 }

 public static void main(String[] args) {
  String[] a = In.readStrings();
  sort(a);
  assert isSorted(a);
  show(a);
 }
}

此代码失败的输入是: 7 4 5 10 22 31 1 90 55 6 4 32 123 90

请协助突出我在这里失踪的内容。

由于

0 个答案:

没有答案