I have an array which contains some elements.
How by doing arr[::-1] sort the entire array?
What is the logic behind that?
This is extended slice syntax. It works by doing [begin:end:step] - by leaving begin and end off and specifying a step of -1, it reverses a string. Example:
>>> 'hello world'[::-1]
'dlrow olleh'
See also
arr[::-1]does not sort a list, it only reverses a list.arr[i] <= arr[i+1].arr[::-1]doesn't do that, it just reverses the order in which they occur in the array.