Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

avector index out of bound issue C++ #103

Open
AbdulBasee opened this issue Feb 7, 2021 · 0 comments
Open

avector index out of bound issue C++ #103

AbdulBasee opened this issue Feb 7, 2021 · 0 comments

Comments

@AbdulBasee
Copy link

AbdulBasee commented Feb 7, 2021

vector shortBubbleSort(vector avector){ //the vector for bubble sort
bool exchanges = true;
int passnum = avector.size();
//while vector size is greater than 0 and exchanges = true
while (passnum > 0 && exchanges) {
exchanges = false;
//loops through vector, exchanging values until it reaches the end of vector.
for(int i = 0; i < passnum; i++){
if(avector[i] > avector[i+1]){
exchanges = true;
int temp = avector[i];
avector[i] = avector[i+1];
avector[i+1] = temp;
}
}
//subtracts from the passnum variable so that the next passthrough is one less
//than the previous, because the largest value has already 'bubbled' all the way up.
passnum = passnum - 1;
}
return avector;

The comparison of avector[i] with avector[i + 1] is going to create an issue when we are comparing the last element. The error will be as follows: "Invalid read of size 4" for avector of size four.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant