希尔排序:排序时间大Obiao表示预估为O(N^(3/2)) 到 O(N^(7/6))
30万数据耗时: 1276
代码如下:
private long[] longArr;
private int nElems;
public ShellSort(int size) {
longArr = new long[size];
nElems = 0;
}
public void insert(long value) {
longArr[nElems++] = value;
}
public void display() {
for (int i = 0; i < nElems; i++) {
System.out.print(longArr[i] + " ");
}
System.out.println(" ");
}
public void shellSorted() {
// 根据数据长度获取初始排序增量,最终增量为1,即为插入排序 ,公式为:h = 3 * h + 1
int h = 1;
while (h <= nElems / 3) {
h = 3 * h + 1;
}
int in;
int out;
long temp;
while (h > 0) {
for (out = h; out < nElems; out++) {
temp = longArr[out];// 固定右侧对比数据
in = out;
// 始终与最右侧数据进行对比,依靠out的递增,当out等于4的时候,先完成0 和 4的比较,当out++等于8的时候,在完成0,4,8的比较(此处默认数据项是10进行分析)
while (in > h - 1 && longArr[in - h] >= temp) {
longArr[in] = longArr[in - h];
in -= h;// 始终保持数据往下找,进行对比
}
longArr[in] = temp;// 比较截止的索引就是右侧的索引
}
h = (h - 1) / 3;// 根据公式减去增量
}
}
18万+

被折叠的 条评论
为什么被折叠?



