【问题标题】:Java Shell Sort Algorithm Causing Infinite Loop: What Am I doing Wrong?Java Shell 排序算法导致无限循环:我做错了什么?
【发布时间】:2015-09-01 13:52:46
【问题描述】:

好的,所以我正在尝试为分配编写一个基本的 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;
        }

【问题讨论】:

  • 你应该做一些调试。

标签: java infinite-loop shellsort


【解决方案1】:

for(gap = size/2; gap

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

【讨论】:

    【解决方案2】:

    这一行:for(gap = size/2; gap &lt;= 0; gap = gap/2) 应该是

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

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 1970-01-01
      • 1970-01-01
      • 2017-07-05
      • 2021-09-19
      • 1970-01-01
      • 2018-05-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多