目录
要求代码复杂度时间复杂度空间复杂度
稳定性
要求
将数值数组按从小到大排序。
代码
public class Shell {
public static void sort(Comparable
[] a
) {
int N
= a
.length
;
int h
= 1;
while(h
<N
/2) {
h
= h
* 2 + 1;
}
while(h
>=1) {
for(int i
=h
;i
<N
;i
++) {
for(int j
=i
;j
>=h
;j
-=h
) {
if(greater(a
[j
-h
],a
[j
])) {
exch(a
, j
, j
-h
);
}else {
break;
}
}
}
h
/= 2;
}
}
private static boolean greater(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
;
}
}
public class Test {
public static void main(String
[] args
) {
Integer
[] a
= {4,8,6,1,3,5,6};
Shell
.sort(a
);
System
.out
.println(Arrays
.toString(a
));
}
}
[1, 3, 4, 5, 6, 6, 8]
复杂度
时间复杂度
最好情况O(n),最坏情况O(n^2),平均O(n ^ 1.3)
空间复杂度
O(1)
稳定性
不稳定
转载请注明原文地址: https://lol.8miu.com/read-34630.html