-4

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.

Azat Ibrakov
  • 9,998
  • 9
  • 38
  • 50
  • 3
    Welcome to Stack Overflow! Please **[edit]** your question with an [mcve] or [SSCCE (Short, Self Contained, Correct Example)](http://sscce.org) – NathanOliver Oct 29 '18 at 16:37
  • Hello, please post your actual code, not an image of it. To understand what's wrong with it we'll have to copy it and make test. That can't be done with an image. – GPhilo Oct 29 '18 at 16:39
  • 1
    @OP -- It is much more work to create an image, save it as a png, link to it, than to simply take text and copy it to the edit window. Why are you doing all of that unnecessary work to post your program? – PaulMcKenzie Oct 29 '18 at 16:45
  • As to your program, you don't check at all what `L` and `R` are, yet expect `std::sort` to work with these values. There is nothing wrong with `std::sort` *if* you supply the correct arguments. – PaulMcKenzie Oct 29 '18 at 16:48
  • Dear sir, L and R are inputs to be entered by user itself, I am afraid I am unable to understand. This is also my first time asking a question so I apologize for the inconvenience you went through. –  Oct 29 '18 at 16:50
  • So if the user enters bogus values, you will still try to call `std::sort` on them without checking them. Also, [don't use that bits header](https://stackoverflow.com/questions/31816095/why-should-i-not-include-bits-stdc-h). Use the proper `#include` files. – PaulMcKenzie Oct 29 '18 at 16:55
  • The problem which I am currently solving has explicitly mentioned that L<=R<=N –  Oct 29 '18 at 17:14
  • @MrigankBadola what if the user did not read the problem statement? You should validate user input always. – 463035818_is_not_an_ai Oct 29 '18 at 17:47
  • @MrigankBadola - `cin>>L>>R` -- What if I don't follow the orders of your problem? This line allows me to put in any nonsense for `L` and `R`. Nothing stops me or anyone else from entering garbage values -- what if I have fat fingers and enter a mistake? If you were reading from a file, where the file has been properly vetted for bogus values, *then* that would be more of a realistic reason to not check values. – PaulMcKenzie Oct 29 '18 at 17:58
  • @PaulMcKenzie for fat fingers i'd recommend something like [this](https://www.youtube.com/watch?v=OqjF7HKSaaI) – 463035818_is_not_an_ai Oct 29 '18 at 19:10
  • @user463035818 Ah, you've got a sense of humour, I like it. –  Oct 29 '18 at 22:41
  • @PaulMcKenzie I acknowledge your fact that you're trying to make my code as clean as possible. As of now, I would request you to cast your vote for the deletion of the question. I thank you and others that spent their valuable time on this rather simple question. I hope the next time I ask a question, it would be well formatted, easy to understand and hopefully not off-topic. –  Oct 29 '18 at 22:50

1 Answers1

3

The second parameter (last) is pointing one past the last element that should be sorted. So when the iterators you pass to sort are

3 -1 4 2 -1 
     ^-------- first
       ^------ last

Then there is only a single element in the range to be sorted and the output you get is to be expected.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185