简体   繁体   中英

Bubble Sort Using Slides instead of swaps

currently I'm being asked to design four sorting algorithms (insertion, shell, selection, and bubble) and I have 3 of the 4 working perfectly; the only one that isn't functioning correctly is the Bubble Sort. Now, I'm well aware of how the normal bubble sort works with using a temp var to swap the two indexes, but the tricky part about this is that it needs to use the array index[0] as a temp instead of a normal temp, which is used in swapping, and slide the lower array variables down to the front of the list and at the end of the pass assign the last index to the temp which is the greatest value.

I've been playing around with this for a while and even tried to look up references but sadly I cannot find anything. I'm hoping that someone else has done this prior and can offer some helpful tips. This is sort of a last resort as I've been modifying and running through the passes with pen and paper to try and find my fatal error. Anyways, my code is as follows...

void BubbleSort(int TheArray[], int size)
{
    for (int i = 1; i < size + 1; i++)
    {
        TheArray[0] = TheArray[i];
        for (int j = i + 1; j < size; j++)
        {
            if (TheArray[j] > TheArray[0])
                TheArray[0] = TheArray[j];
            else
            {
                TheArray[j - 1] = TheArray[j];
            }
        }
        TheArray[size- 1] = TheArray[0];
    }
}

Thanks for any feedback whatsoever; it's much appreciated.

If I understand the problem statement, I think you're looking for something along these lines :

void BubbleSort(int theArray[], int size)
{
    for (int i = 1; i < size + 1; i++)
    {
        theArray[0] = theArray[1];
        for (int j = 1; j <= size + 1 - i; j++)
        {
            if (theArray[j] > theArray[0])
            {
                theArray[j-1] = theArray[0];
                theArray[0] = theArray[j];
            }
            else
            {
                theArray[j - 1] = theArray[j];
            }
        }
        theArray[size-i+1] = theArray[0];
    }
}

The piece that you're code was missing, I think, was that once you find a new maximum, you have to put it back in the array before placing the new maximum in theArray[0] storage location (see theArray[j-1] = theArray[0] after the compare). Additionally, the inner loop wants to run one less each time since the last element will be the current max value so you don't want to revisit those array elements. (See for(int j = 1 ; j <= size + 1 - i ; j++))

For completeness, here's the main driver I used to (lightly) test this :

int main()
{
    int theArray[] = { 0, 5, 7, 3, 2, 8, 4, 6 };
    int size = 7;

    BubbleSort(theArray, size);
    for (int i = 1; i < size + 1; i++)
        cout << theArray[i] << endl;
    return 0;
}

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