+ 1
How to improve this code?
I wrote a algorithm to sort a list of numbers. How can I improve it's efficiency? The code gives recursion depth error sometimes. How do I solve this? https://code.sololearn.com/c394XwSN190W/?ref=app
17 Answers
+ 6
There are a lot of sorting algorithms, both efficient and not efficient.
You can search for different algorithms and see which is good for you. But more efficient algorithms are often harder to understand and implement.
Quick sort is the most efficient algorithm amongst popular ones, as far as I know. I remember answering to one question where I have mentioned a StackOverflow thread on the most efficient algorithms, I will link it here if I find it.
But no matter which algorithm you use, there are always limits for them in Sololearn Playground (either recursive or runtime).
[edit]
Not exactly that, but just a good thread: https://stackoverflow.com/questions/10604693/fastest-sorting-technique .
The most efficient algorithm in terms of time complexity will have execution time of O(n×log(n)) in the worst case.
[edit 2]
According to Wikipedia (https://en.wikipedia.org/wiki/Sorting_algorithm ), the best algorithm in terms of speed, memory and stability might be block sort. You can read about it and try it.
+ 4
Brian setting recursion limit isn't working. I set the limit to 10,000 and still it says RecursionError because a certain line was repeated 996 times
+ 4
As far as I know about sorting in Python :
To sort a short list (under 50 elements) you should probably use the insertion sort.
For more than 50 elements, merge sort may be a lot quicker. O(nlog(n))
+ 3
In case you haven't already tried this, Python recursion depth can be adjusted. Default is 1000.
import sys
sys.setrecursionlimit(<enter your custom depth limit here>)
+ 3
Harsha S, so, it turns out that the recursion limit is either deeper than the sys module's control or the server/interpreter automatically sets it to 1000 after each change.
+ 3
get I get it
will try in other compiler
+ 3
Harsha S you can see my improvement here:
https://code.sololearn.com/cu5O4WocK6Ad/?ref=app
+ 2
https://www.sololearn.com/learn/774/?ref=app
+ 2
Harsha S I tweaked your code and this is what I came up with (doesn't use recursion)
https://code.sololearn.com/c76iz8wg46EP/?ref=app
+ 2
Brian I didn't know this!
Thanks!!
+ 1
Simon Sauter thank you
- 4
C programming
- 6
سلام
- 7
Hii
- 7
Hi