修改冒泡排序算法的问题

时间:2015-09-01 00:10:09

标签: java arrays sorting integer

好的我正在尝试编写一个用于排序数组的算法,在本例中是一个随机整数数组。我知道QuickSort或类似的显然会更有效,但对于这个任务,我必须基本上做一个低效的冒泡排序算法的修改版本。

这个想法是比较差距中的整数。每次通过后,间隙应该减半。如果左边的值大于右边的值,则交换它们。执行应该继续,直到没有掉期或差距是1.我通常在这种事情上很下降,但似乎我错过了一些东西。由于某种原因,算法没有对我的数组进行排序。

这是我的代码。也许有人可以看到我所缺少的东西:

public class ShellArray
{
    private int capacity;
    private int [] randArray;
    private static final int RANGE = 200;               //Range set to 200 for possible integers.

    public ShellArray(int capacity)
    {
        this.capacity = capacity;
        randArray = new int[capacity];
        populate(randArray, RANGE, capacity);
    }

    /**************************************************************************************************************************************************
    //
    //Populates array with random integers within given range.
    //
    ***************************************************************************************************************************************************/

    private static void populate(int [] myArray, int numRange, int extent)
    {
        Random r = new Random();

        for (int i = 0; i < extent; i++)
            myArray[i] = (r.nextInt(numRange)+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;
    }

    public String toString()
    {
        String result = "";

        for(int y = 0; y < randArray.length; y++)
            result += randArray[y] +" ";

        return result;
    }

}

1 个答案:

答案 0 :(得分:0)

您尚未提供作业的详细信息,但按照这些方式进行排序的想法是,最后阶段(即使用gap == 1)是真正的标准排序本身 - 在这种情况下,我猜一个冒泡排序 - 前面的阶段预先处理输入,以便最终排序的效率比随机输入好得多。至少对于gap == 1,您必须重复排序循环,直到没有掉期为止。

这里有两种主要的变化,但您还没有告诉我们您想要的信息:

  • 第一个变体在每次迭代循环后缩小间隙,直到达到1,然后重复该差距,直到没有更换掉。
  • 第二种变化以相同的间隙重复排序循环,直到没有掉期,然后收缩间隙并重复。

我的猜测是你想要第二个,因为它实际上是一个shell排序(具有不同寻常的味道)。即使它听起来效率低于另一个,但它很有可能更多效率,因为当差距为时,需要投入少量额外的努力。 large以更少的步骤完成更大的元素移动。