Doubt in output

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    int a[10]= { 1, 5, 8, 9, 6, 7, 3, 4, 2, 0 };
    sort(a, a+5, greater<int>());
    for(int i=0;i<10;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}

Shouldn’t this code give numbers in descending order?
It is giving some weird output. Can you run this and explain? This question is from the STL quiz

hello @PranavM16

check this line .
here we are passing legnt as 5 i.e means it will sort on first five elements in descending , rest will remain at same position

1 Like