Skip to content

avector index out of bound issue C++ #103

Description

@AbdulBasee

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions