The std::sort
function is somehow not able to sort a particular part of a vector. The upper bound and lower bound of the part of the vector to be sorted are entered as inputs along with size of vector and vector elements. This is what I've tried:
#include<iostream>
#include <vector>
#include<algorithm>
using namespace std;
int main()
{
long long int N,L,R;
cin>>N;
vector <long long int> V;
while(N--)
{
long long int input;
cin>>input;
V.push_back(input);
}
cin>>L>>R;
sort(V.begin()+L-1,V.begin()+R-1);
vector<long long int>::iterator I = V.begin();
while(I<V.end())
{
cout << *I << " ";
I++;
}
cout << endl;
}
My input:
5
3 -1 4 2 -1
3 4
Expected output:
3 -1 2 4 -1
Actual output:
3 -1 4 2 -1 (unchanged)
Kindly tell what is incorrect in this approach/ what other method can be applied.