Doubt in sort( )

Hello, I understand that why we write sort (a, a+n) and don’t write it like sort (a, a+n-1) .

But what does it mean sort (a, a+n)

Why we don’t write sort ( a[0], a[n] )

See , when we write sort (a, a+n) it means that we want iur sort function to run from a which is a[0] to n space blocks more of data type of a. In simple words,suppose you have an array a[5] ={1,2,3,4,5} here when you will write
sort (a, a+n) Compiler will understand , ki bhai ab humko a[0] se 5 more steps pr operate krna hai.
And why we dont write sort (a, a[n]) is because , sort function doesn’t take it as a parameter. It only takes array and it’s size.

So, aren’t both the same thing i.e. sort (a, a+n) and sort (a[0], a[n]).
Is it taking the address of array which is same as taking the address of first block of array.

If your array is a={1,-3,5,-2,2}
Here your n is 5
So when you will send sort (a, a[n]) it means that sort (a, 2) because your a[n] is 2 so your work function will work till 1,-3,5 and not on the rest of the array. Moreover that what if at a[n] there’s a negative integer . It will make a blunder. I guess this will help you understanding it :slight_smile:

1 Like