the sort function take the two parameter one is the array that u what to sort and the sent is length how the length is calculate here arr+5 : arr means the zero index how is evaluated?
Sort() function how the 2nd paramter is evaluated
Hello @Vikaspal
we sort an array using the inbuilt sort method like this
sort(para1, para2)
Where the first parameter denotes the start element and second parameter denotes the end element
If we have an array say arr[] = {1, 5, 3, 4, 2, 7, 8, 9} and if we want to sort the indices 1 to 4 (0-based indexing), then we would say
sort(arr + 1, arr + 5);
where arr + 1 points to (contains the address of) element 5 and arr + 5 points to (contains the address of) element 7
If you still need any help, leave a reply to this thread and I will help you out.
okay i got your point clearly the sort () accept the start and the end point where to want to stop but suppose arr[] = {1, 5, 3, 4, 2, 7, 8, 9} i want to till the 7 elements i can sort(arr,start,end) : where arr is the arr that you want to sort the another two parameter are the start and the end indices. but it seems confused because in your code sort(arr+1,arr+7) , we have to pass the array name also and the indices with combination.
Hello @Vikaspal
This sort function is predefined in the c++ libraries.
The sort is function is defined to accept two parameters => The starting point from where you want to sort and the end point (till where you want to sort).
So if I want to sort the whole array arr[] = {1, 5, 3, 4, 2, 7, 8, 9} I will say
sort(arr + 0, arr + 8) which is equivalent to sort(arr, arr + 8).
We send the array in combination with indices because
arr contains the address of the first element
arr + 1 contains the address of the second element
arr + 2 contains the address of the third element
…
arr + 8 contains the address of the ninth element (NOTE: - our array contains only 8 elements but we send the address of the ninth element (the next element) as the second parameter, this is predefined).
so when I say sort(arr + 3, arr + 5) it sorts the array from fourth element to fifth element
Generally
To sort the whole array we use
sort(arr, arr + size); // Where size is the number of elements in the array.
If you still need any help, leave a reply to this thread and I will help you out.
thanks i got it you have briefly defined me thanks
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.