我要写一个基本的shell排序算法。目标是对随机整数数组进行排序。计划是:在第一次传递中,间隙是数组大小的一半。对于每个后续的通道,间隙大小被削减一半。对于最后一次传递,间隙大小为1,因此它将与冒泡排序相同。传递将继续,直到没有交换发生。然而,我得到了一个无限循环。有人知道问题出在哪里吗?
下面是我的方法和它用于交换的服务方法的两个版本:
/**************************************************************************************************************************************************
//
//The first version of shellSort calls the second version with min value as 0 and max as length of randArray-1. Takes no parameters.
//
***************************************************************************************************************************************************/
public void shellSort()
{
shellSort(0, randArray.length-1);
}
/**************************************************************************************************************************************************
//
// shellSort which takes min and max parameters. Calculates gap at center, across which values are compared. Passes continue until gap size is 1
// and array is sorted.
// Uses boolean sorted to indicate when array is sorted so passes don't continue needelessly after array is sorted. Essentially, if no values
// are swapped after a pass, we know array is sorted and sorted is not set to false.
//
// Outer for loop controls position of final value. Since largest value is bubbled to end, position decreases by 1 after each pass.
// After each pass, size of gap is cut in half, as long as gap is 2 or greater. Otherwise gap would become too small.
// Inner for loop controls the index values to be compared.
// Uses swap method to swap values which are not in the correct order.
// Array is printed after each pass.
//
***************************************************************************************************************************************************/
public void shellSort(int min, int max)
{
String result;
int gap;
int j = 0;
int size = randArray.length-1;
boolean swapped;
for(gap = size/2; gap <= 0; gap = gap/2)
{
swapped = true;
while (swapped)
{
swapped = false;
int comp;
for(comp = 0; comp+gap <= size; comp++)
{
if (randArray[comp] > randArray[comp+gap])
{
swap(comp, comp+gap);
swapped = true; //swapped set to true if any element is swapped with another.
}
else
swapped = false;
}
}
result ="";
for(int y = 0; y < randArray.length; y++)
{
result += randArray[y] +" ";
j++;
}
System.out.println("Pass " +j+": " +result+"n");
}
}
/**************************************************************************************************************************************************
//
// Swaps two values in the array.
//
***************************************************************************************************************************************************/
private void swap(int index1, int index2)
{
int temp = randArray[index1];
randArray[index1] = randArray[index2];
randArray[index2] = temp;
}
for(gap = size/2;Gap <= 0;Gap = Gap/2)
是问题所在。如果gab为0或以下,则会导致无限循环。
这一行:for(gap = size/2; gap <= 0; gap = gap/2)
应该
for(gap = size/2; gap > 0; gap = gap/2)