考虑以下JAVA排序代码,对于array为{15,0,6,9,3}时,运行...
发布于 2022-03-03 13:57:15
考虑以下JAVA排序代码,对于array为{15,0,6,9,3}时,运行sort方法,则最终排序结果为:
public void sort(Comparable[] a) {
int N = a.length
int h = 1
while (h < N / 3) {
h = 3 * h + 1// 1, 4, 13, 40, ...
}
while (h >= 1) {
for (int i = h i < N i++) {
for (int j = i j >= h && compareElement(a[j], a[j - h]) j -= h) {
exch(a, j, j - h)
}
}
h = h / 3
}
}
public boolean compareElement(Comparable v, Comparable w) {
return v.compareTo(w) < 0
}
public static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i]
a[i] = a[j]
a[j] = t
}
public void sort(Comparable[] a) {
int N = a.length
int h = 1
while (h < N / 3) {
h = 3 * h + 1// 1, 4, 13, 40, ...
}
while (h >= 1) {
for (int i = h i < N i++) {
for (int j = i j >= h && compareElement(a[j], a[j - h]) j -= h) {
exch(a, j, j - h)
}
}
h = h / 3
}
}
public boolean compareElement(Comparable v, Comparable w) {
return v.compareTo(w) < 0
}
public static void exch(Comparable[] a, int i, int j) {
Comparable t = a[i]
a[i] = a[j]
a[j] = t
}
登录后免费查看答案
关注者
0
被浏览
16