logic behind sorted() function in python
Python's sorted () is written in C and compiled to native code. When you write your own sort, it's run entirely in the python interpreter, which is at least an order of magnitude slower - nearly two. It's hard to say without seeing your code, but there's also a chance that your Python sort is doing a bunch of allocation behind the scenes...
I have an array of 2k elements and i am trying to sort it.
When i call sorted(arr) it takes very less time, i.e 3-4 sec to sort an array.
but when i am building my own sorting function(merge sort) with time complaxity nlogn it is taking more then a min to sort it.
I also tried with quick sort and bubble sort but none of the algo is able to reduce the time to this extent.
I am not able to understand that how builtin function works and how it is taking so less time. what is the logic behind it?