冒泡排序Java不同的输出

时间:2016-08-30 03:16:16

标签: java intellij-idea

我遇到了一个非常令人困惑的情况,我写了这个BubbleSort程序并且运行得很好。输出正确:

public class BubbleSortInput {

private static void Sorting(int[] intArray)
{
    int i, temp=0;
    int n = intArray.length;

    for(i=0; i < n - 1; i++)
    {
        for(int j = 0; j < n-i-1; j++)
        {
            if(intArray[i]>intArray[i+1])
            {
                temp = intArray[i+1];
                intArray[i] = intArray[i+1];
                intArray[i] = temp;
            }
        }
    }
}

public static void main(String[] args) {

    int array[] = {1,5,65,34,76,234};

    Sorting(array);
    for(int k = 0; k < array.length; k++)
    {
        System.out.println(array[k]);
    }
}
}

但是,我尝试在main方法中在另一个类中编写基本相同的代码:

class BubbleSort {
public static void main(String[] args) {
   int numbers[] = {12,43,65,12,65,92,32,54};
   int i,temp=0;

    for(i=0; i < numbers.length-1; i++)
    {
        for(int j = 0; j < numbers.length-i-1; j++)
        {
            if(numbers[i]>numbers[i+1])
            {
                temp = numbers[i+1];
                numbers[i] = numbers[i+1];
                numbers[i]= temp;
            }
        }
    }

    for(i=0;i<numbers.length;i++)
    {
        System.out.println(numbers[i]);
    }
}
}

我在第二个文件上输出的输出是完全错误的,即使我使用了几乎相同的代码,有人可以解释一下吗?

Output: 

12
43
12
12
65
32
32
54

3 个答案:

答案 0 :(得分:0)

我认为你的冒泡代码是正确的。这是你的循环:

for(i=0; i < n - 1; i++)
{
    for(int j = 0; j < n-i-1; j++)
    {
        if(intArray[i]>intArray[i+1])
        {
            temp = intArray[i+1];
            intArray[i] = intArray[i+1];
            intArray[i] = temp;
        }
    }
}

请注意,您永远不会使用变量j。所以它只是循环遍历数组,然后交换两个元素,如果第一个元素大于第二个元素。您应该再次查看冒泡排序算法并重新编写代码。

答案 1 :(得分:0)

正如其他人所指出的那样,你应该看一下泡泡排序算法。只是提醒一下,在说明原始代码正常工作之前运行许多测试用例。需要明确的是,第一个程序也提供了错误的输出。您可能为输入集获得的输出可能是真的,但这有点排序开始。尝试在第二个程序中使用的输入集作为第一个代码并识别错误。另外,请查看for循环中的交换代码。

                temp = intArray[i+1];
                intArray[i] = intArray[i+1];
                intArray[i] = temp;

您将[i + 1]位置的值指定为temp。并且您再次将[i + 1]处的值分配给位置i。因此,位置[i]的值在此过程中丢失了。现在位置[i]和[i + 1]的值相同。所以也要努力。

除此之外。在冒泡排序中,排序通过交换相邻元素来进行。因此,在第一次传递结束时(升序排序),数组中最大的元素将在最后。这个过程一直持续到所有元素都被排序。

Example:
First Pass:
( 6 1 3 2 9 ) –> ( 1 6 3 2 9 ), Here, algorithm compares the first two elements, and swaps since 6 > 1.
( 1 6 3 2 9 ) –>  ( 1 3 6 2 9 ), Swap since 6 > 3
( 1 3 6 2 9 ) –>  ( 1 3 2 6 9 ), Swap since 6 > 2
( 1 3 2 6 9 ) –> ( 1 4 2 5 8 ), Now, since these elements are already in order (9 > 6), algorithm does not swap them.
Second Pass:
( 1 3 2 6 9 ) –> ( 1 3 2 6 9 )
( 1 3 2 6 9 ) –> ( 1 3 4 6 9 ), Swap since 3 > 2
( 1 2 3 5 9 ) –> ( 1 2 3 5 9 )
( 1 2 3 5 9) –>  (1 2 3 5 9 )
Now, the array is already sorted, but our algorithm does not know if it is completed. The algorithm needs one whole pass without any swap to know it is sorted.
Third Pass:
( 1 2 3 5 9 ) –> (1 2 3 5 9 )
( 1 2 3 5 9 ) –> ( 1 2 3 5 9 )
( 1 2 3 5 9 ) –> ( 1 2 3 5 9 )
( 1 2 3 5 9 ) –> ( 1 2 3 5 9 )

答案 2 :(得分:0)

The sorting logic you are using is incorrect.

Bubble sort compares each pair of adjacent items and swaps them if they are in the wrong order (not in ascending order). By the end of the first pass(ascending order sorting), the largest element in the array will be at the last index. By the end of the second pass(ascending order sorting), the second largest element in the array will be at the second last index and so on.....

Visit http://visualgo.net/sorting for better understanding.

So, in your code you should compare the items with the 2nd initialization (variable j in your case). After modification it should look like:

for(i=0; i < n - 1; i++)
{
    for(int j = 0; j < (n-i)-1; j++)
    {
        if(intArray[j]>intArray[j+1])
        {
            temp = intArray[j];
            intArray[j] = intArray[j+1];
            intArray[j+1] = temp;
        }
    }
}