Case 1 Input
10
1 2 3 1 2 3 1 2 3 1 2 3
Case 1 Output
a.out: malloc.c:2401: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >
= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
Case 2 Input
4
1 2 3 1 2 3 1 2 3 1 2 3
Case 2 Output
1 1 2 3
I very well know that in both cases I am giving extra integers as input. My question is why the code is giving an error in Case 1 and not in Case 2. I am compiling my code with g++ -pipe -O2 -std=c++11 ./filename.cpp. There are no errors or warnings. I even tried replacing vector<int> arr(right)
with vector<int> arr(1000000)
for Case 1 still same error. The first line is the size of the array. Next line is elements of the array. Replacing all temp[...]
and arr[...]
with temp.at(...)
and arr.at(...)
I am getting following error in Case 2.
terminate called after throwing an instance of 'std::out_of_range'
what(): vector::_M_range_check: __n (which is 6) >= this->size() (which is 6)
Aborted (core dumped)
#include<iostream>
#include<vector>
using namespace std;
void quick_sort_3(vector<int> &arr,int &left,int &right)
{
if(left<right)
{
vector<int> temp(right-left+1);
int pivot=arr[left];
int small=left,large=right;
for(int i=left+1;i<=right;i++)
{
if(arr[i]<pivot)
temp[small++]=arr[i];
else if(arr[i]>pivot)
temp[large--]=arr[i];
}
for(int i=left;i<small;i++)
arr[i]=temp[i];
for(int i=small;i<=large;i++)
arr[i]=pivot;
for(int i=large+1;i<=right;i++)
arr[i]=temp[i];
small--;
large++;
quick_sort_3(arr,left,small);
quick_sort_3(arr,large,right);
}
}
int main(void)
{
int left=0,right;
cin>>right;
vector<int> arr(right);
right--;
for(int i=0;i<=right;i++)
cin>>arr[i];
quick_sort_3(arr,left,right);
for(int i=0;i<=right;i++)
cout<<arr[i]<<" ";
cout<<endl;
return 0;
}