简体   繁体   中英

Java - Why is my Bubble Sort faster than my Insertion Sort?

I'm measuring the number of steps it takes for sorts to complete sorting and for whatever reason, bubble sort always seems to be a tad quicker than insertion sort and from what I read, it should be the other way around.

Not going to post my full code, but I believe the issue may lie with where I have my counts.

Bubble sort:

public void sort()
{
    for (out = nElems-1 ; out >= 1 ; out--)
    {
        count = count+1;
        for (in = 0 ; in < out ; in++)
        {
            if ((a.get(in)) > (a.get(in+1)))
            {
                swap (in, in+1);
                count = count+2;
            }
        }
    }
}

Insertion Sort:

void sort()
{
    Integer temp[] = new Integer[1];
    for (out = 1 ; out < nElems ; out++)
    {
        count = count+1;
        temp[0] = a.get(out);
        in = out;
        while (in > 0 && a.get(in-1) >= temp[0])
        {
            a.set(in, a.get(in-1));
            --in;
            count = count+2;
        }
        a.set(in, temp[0]);
    }
}

and just to give an example, I sorted 3 text files filled with 2000 random integers with values from 1-2000, the average for insertion sort was 2,007,677 steps, and bubble sort's average was 2,005,719.

So, first of all, you're only measuring assignments, not overall performance. Secondly, your insertion sort has the optimization of not swapping to move the value over, so why are you adding 2 to count every time? Your while loop should only be adding 1 to count , and then you should add 1 again outside the while loop when you have the a.set(in,temp[0]) . (Side note - why is temp an array and not just a single Integer?) Now, if you want to measure overall performance, you need to measure comparisons and assignments (preferably in two separate variables, as certain devices/architectures can have very different performance for those operations).

Edit:

To clarify, a comparison is when you compare the values of two items from your array with an operator like less than (<) or greater than (>). An assignment is when you change a value within an array (in your case, using a.set() ).

Here's some code modifications (I split count into two variables that represent the actual things you'll want to measure, assignments and comparisons ):

Bubble sort:

public void sort()
{
    for (out = nElems-1 ; out >= 1 ; out--)
    {
        for (in = 0 ; in < out ; in++)
        {
            comparisons = comparisons+1;
            if ((a.get(in)) > (a.get(in+1)))
            {
                swap (in, in+1);
                assignments = assignments+2;
            }
        }
    }
}

Insertion sort:

void sort()
{
    int temp = 0;
    for (out = 1 ; out < nElems ; out++)
    {
        count = count+1;
        temp[0] = a.get(out);
        in = out;
        comparisons = comparisons + 1; //You're doing the comparison below at least once
        while (in > 0 && a.get(in-1) >= temp[0])
        {
            a.set(in, a.get(in-1));
            --in;
            assignments = assignments+1;
            comparisons = comparisons + 1; 
        }
        a.set(in, temp[0]);
        assignments = assignments+1;
    }
}

One other thing to consider - where are you initializing the variable count ? I don't see it in your code, and it could actually be that you're using the same variable from a different scope, and since you don't reset it to 0, whichever sort you're doing second is adding onto the number from your first one.

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