Home > OS >  How to handle this odd and even bubble sorting error?
How to handle this odd and even bubble sorting error?

Time:12-24

I wanna ask why the even side of the odd&even bubble sort will raise a zsh:abort error in VScode? Is it because it is out of range? If so, does that mean that I have to precisely modify the range? Thank you!

#include <iostream>
using namespace std;

int main()
{
    int a[10];
    for (int i=0;i<10;i  )
    {
        cin>>a[i];
    }
    //First, sort odd and even numbers
    int l=0,r=9;//point to the two ends of the array
    while (l<=r)
    {
        bool leftIsOdd=a[l]%2==1;
        bool rightIsEven=a[r]%2==0;
        //move the two pointers from ends to middle
        if (leftIsOdd)
        {
            l  ;
        }
        else if (rightIsEven)
        {
            r--;
        }
        //since it's a symmetric array, with 5 odd and 5 even, we can swap when both sides get stuck
        //Q:If we have 4 odd numbers and 6 even numbers, is the approach OK?
        else if (!leftIsOdd && !rightIsEven)
        {
            int temp=a[l];
            a[l]=a[r];
            a[r]=temp;
        }
    }
    //perform bubble sort for left odd part
    int start=0,end=l;
    for (int i=start; i<end-1;i  )
    {
        for (int j=start 1;j<end-i;j  )
        {
            if (a[j-1]>a[j])
            {
                int temp=a[j];
                a[j]=a[j-1];
                a[j-1]=temp;
            }
        }
    }
    //now bubble the right even side
    start=l,end=10;
    for (int i=start; i<end-1;i  )
    {
        for (int j=start 1;j<start end-i;j  )
# # #         //Why j<start end-1 would produce error?
        {
            if (a[j-1]>a[j])
            {
                int temp=a[j];
                a[j]=a[j-1];
                a[j-1]=temp;
            }
        }
    }
    for (int i=0;i<10;i  )
    {
        cout<<a[i]<<' ';
    }
    return 0;
}

I tried putting index j out of the expected range, and received zsh:abort error.

CodePudding user response:

If you initialize the input array a with 10,9,8,7,6,5,4,3,2,1 as you mentioned then when you reach the "// now bubble the right even side" loop you will initialize start to 5 and end to 10. If you write j<start end-1 then this will allow j to be as large as 13. (i.e. j must be less than 14). You will then try to access a[13], but a has only 10 elements. Therefore, if you are lucky, you will get some kind of memory access error when you run the program and it will crash.

I don't know why you want to replace the expression j<start end-i (which seems to work, with the expression j<start end-1 which causes a crash.

You could probably make the crash happen a lot easier just by including the line a[13]=0; after you declare and initialize a.

When you are using arrays it is your responsibility to ensure that all array accesses are valid. When you ask "does that mean that I have to precisely modify the range" the answer is "yes, definitely!"

  • Related