我正在尝试实现 Shell 排序。了解 Shell Sort 将排序分解为分段插入,我使用值 10 首先除以 5 来开始第一个分段。然后 2 表示第二次分段,然后 1 表示最后一次分段。问题在于 1 的最后一个分段。 if 语句没有检查数组的第一个元素。当使用大小为 12 的数组时,array[0] 和 array[10-11] 发现自己没有排序。我似乎无法在分段插入排序中为我的 for 循环或 while 循环找到正确的起点。10 个未排序的数组:10 9 8 7 6 5 4 3 2 1 已排序:10 1 2 3 4 5 6 7 8 9 12 个未排序的数组:12 11 10 9 8 7 6 5 4 3 2 1 已排序:12 1 2 3 4 5 6 7 8 9 10 11 编辑:: 我认为问题不在于分割方面。将排序的最终段间隙视为 1。 n = size; h = 段;
public int SegmentedInsertionSort(IntegerType[] A, int n, int h)
{
IntegerType temp;
System.out.println("Entered Segmeted InsertionSort");
for(int -> h+1 to n)
{
int j = i -h;
while( j > 0)
{
if (A[j].isBetterThan(A[j+h]))
{
System.out.println("Swap");
temp = A[j+h];
A[j+h] = A[j];
A[j] = temp;
j=j-h;
}else
{
System.out.println("Ever?");
j = 0;
}
}
System.out.println("Segmenting");
}System.out.println("outter loop done"); return h;
}
public void ShellSort(IntegerType[] A, int size)
{
int H = (size/2);
System.out.println("Entering Shell Sort");
while(H > 0)
{
SegmentedInsertionSort(A, size, H);
H = H/2;
}
}