导致无限循环的Java Shell排序算法:我做错了什么?

时间:2015-09-01 05:44:15

标签: java infinite-loop shellsort

好的,我正在尝试为作业编写基本的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;
        }

2 个答案:

答案 0 :(得分:0)

for(gap = size / 2; gap&lt; = 0; gap = gap / 2)

是问题所在。如果gab为0或更低,则会导致无限循环。

答案 1 :(得分:0)

这一行:for(gap = size/2; gap <= 0; gap = gap/2)应为

for(gap = size/2; gap > 0; gap = gap/2)