简体   繁体   中英

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;
        
                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?

Additionally, can I use bubble sort to sort an 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:

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:

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) ).

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. 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 .

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 . So if your j goes on to the last element of the array, the next element wouldn't exist and raises an exception. Also you can use this code to sort ArrayList . Just change the code swapping values with the appropriate setters and getters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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