简体   繁体   English

Java中的冒泡排序混乱

[英]Bubble sort confusion in Java

I have a question about bubble sort using an array. 我有一个关于使用数组进行冒泡排序的问题。 Here is some example code: 这是一些示例代码:

public class SortArray 
{
    public static void main(String[] args)
    {
        int[] arr = {4,6,4,2,764,23,23};
        sort(arr);
    }
    static void sort(int[] arr)
    {
        int k;
        for(int i = 0; i < arr.length; i++) { for(int j = i; j < arr.length-1; j++) {
                if(arr[i] < arr[j+1])
                {
                    k = arr[j+1];
                    arr[j+1] = arr[i];
                    arr[i] = k;
                }
            }
            System.out.print(arr[i] + " ");
        }   
    }
}

There are two loops for checking the array, I understand the first loop is to go through each element in array, but how about the second one? 有两个检查数组的循环,我知道第一个循环是遍历数组中的每个元素,但是第二个循环又如何呢? Why is j starting from i and why is it going up to the length minus 1? 为什么ji开始,为什么长度达到负1?

Additionally, can I use bubble sort to sort an ArrayList ? 另外,我可以使用冒泡排序对ArrayList进行排序吗?

What you have there is called selection sort , not bubble sort . 您所拥有的称为选择排序 ,而不是冒泡排序

Bubble sort only swaps adjacent elements, but you're going though the rest of the array and finding the minimum, which is exactly what selection sort does. 冒泡排序仅交换相邻的元素,但是您将遍历数组的其余部分并找到最小值,这正是选择排序所要做的。

Since you use arr[j+1] , you can rewrite the inner loop to use arr[j] and shift up the range by one, like this: 由于您使用arr[j+1] ,因此可以重写内部循环以使用arr[j]并将范围向上移动一个,如下所示:

for(int j = i + 1; j < arr.length; j++)
{
    if(arr[i] < arr[j])
    {
        k = arr[j];
        arr[j] = arr[i];
        arr[i] = k;
    }
}

Now it's a bit more clear that you're looking at each other remaining element to find the minimum. 现在,您可以清楚地看到彼此之间剩余的元素以找到最小值。

Yes, you can modify any sort that works on arrays to sort an ArrayList instead by just using ArrayList.get instead of array accesses and ArrayList.set instead of array assignments, ie: 是的,您可以修改任何适用于数组的排序方式来对ArrayList排序,而只需使用ArrayList.get而不是数组访问和ArrayList.set而不是数组分配即可,即:

for(int i = 0; i < arr.size(); i++)
{
    for(int j = i + 1; j < arr.size(); j++)
    {
        if(arr.get(i) < arr.get(j))
        {
            int k = arr.get(j);
            arr.set(j, arr.get(i));
            arr.set(i, k);
        }
    }
}

Note that both selection sort and bubble sort are fairly inefficient ( O(n^2) ), there are more efficient sorting algorithms such as quick-sort or merge-sort (running in O(n log n) ). 请注意,选择排序和冒泡排序都效率很低( O(n^2) ), 还有更高效的排序算法,例如快速排序或合并排序(在O(n log n) )。

The first loop holds an element in i and the second one examines each element after i , namely j and compares it to the i it has holded. 第一个循环在i保存一个元素,第二个循环检查i之后的每个元素,即j ,并将其与已保存的i进行比较。 If they can be swapped, the algorithm swaps their positions and moves on. 如果可以交换它们,则算法交换其位置并继续前进。 When the outer loop finishes an iteration, all elements prior to i are sorted, so j starts from i . 当外循环完成一次迭代时,对i之前的所有元素进行排序,因此ji开始。

Do this once on paper and it all makes sense. 只需在纸上做一次,这一切都是有道理的。

EDIT : As you have completed your algorithm, the reason for the second loops condition, is that you swap i with j+1 . 编辑 :完成算法后,第二个循环条件的原因是您将ij+1交换。 So if your j goes on to the last element of the array, the next element wouldn't exist and raises an exception. 因此,如果您的j继续到数组的最后一个元素,则下一个元素将不存在并引发异常。 Also you can use this code to sort ArrayList . 您也可以使用此代码对ArrayList进行排序。 Just change the code swapping values with the appropriate setters and getters. 只需使用适当的setter和getter更改代码交换值即可。

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

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