简体   繁体   English

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

[英]Java Shell Sort Algorithm Causing Infinite Loop: What Am I doing Wrong?

Ok so I'm trying to write a basic shell sort algorithm for an assignment. 好的,所以我正在尝试为分配编写基本的shell排序算法。 The goal is so sort an array of random integers. 目标是对随机整数数组进行排序。 The plan is: In the first pass, the gap is half the size of the array. 计划是:在第一遍中,间隙是数组大小的一半。 For each subsequent pass, the gap size is cut in half. 对于随后的每遍,间隙尺寸将减半。 For the final pass(es), the gap size is 1, so it would be the same as a bubble sort. 对于最终通过,间隙大小为1,因此它将与气泡排序相同。 The passes continue until no swaps occur. 通行证将继续进行,直到没有交换发生。 However, I'm getting an infinite loop. 但是,我遇到了无限循环。 Can anyone see what the problem is? 谁能看到问题所在?

Here is both versions of my method and the service method it uses for swapping: 这是我的方法的版本以及用于交换的服务方法:

/**************************************************************************************************************************************************
    //
    //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) for(间隙=尺寸/ 2;间隙<= 0;间隙=间隙/ 2)

is the problem. 是问题。 If gab is 0 or below it leads to the infinite loop. 如果gab为0或小于0,则会导致无限循环。

这条线: for(gap = size/2; gap <= 0; gap = gap/2)应为

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM