i'm trying to do insertion sort over vectors in C++ but my ./a.out returns this: * Error in `./insertion': double free or corruption (out): 0x000000000154fc20 *
Don't know why is this happenning, i've seen other problems like this and it's always about the code, buy in my code i don't know what the problem is.
This is the code:
#include<iostream>
#include<vector>
using namespace std;
void insertion(vector<int> v){
int tam = v.size();
int key,j,i;
for(i=1; i<tam; i++){
key = v[i];
j=i-1;
while(j>=0 && v[j]>key){
v[j+1] = v[j];
j--;
}
v[j]=key;
}
}
void print(vector<int> v){
cout<<endl;
for(int i = 0; i<v.size(); i++){
cout<<i+1<<".\t"<<v[i]<<"\n";
}
}
int main(){
cout<<"----------------INSERTION SORT----------------\n\n";
cout<<"\nPlease, fill the vector: \n\n";
vector<int> v;
int a;
bool response = true;
while(response){
cout<<"\nEnter your number: ";
cin>>a;
v.push_back(a);
cout<<"Another?(1/0): ";
cin>>response;
cout<<endl;
}
insertion(v);
print(v);
return 0;
}