How to improve this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

30th Sep 2021, 8:03 PM
Harsha S
Harsha S - avatar
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.
1st Oct 2021, 9:14 AM
#0009e7 [get]
#0009e7 [get] - avatar
+ 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
1st Oct 2021, 5:31 AM
Harsha S
Harsha S - avatar
+ 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))
1st Oct 2021, 10:57 AM
Arthur Le Floch
+ 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>)
30th Sep 2021, 11:38 PM
Brian
Brian - avatar
+ 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.
1st Oct 2021, 6:58 AM
#0009e7 [get]
#0009e7 [get] - avatar
+ 3
get I get it will try in other compiler
1st Oct 2021, 7:07 AM
Harsha S
Harsha S - avatar
1st Oct 2021, 10:48 AM
JaScript
JaScript - avatar
+ 2
https://www.sololearn.com/learn/774/?ref=app
30th Sep 2021, 9:02 PM
Simon Sauter
Simon Sauter - avatar
+ 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
30th Sep 2021, 9:59 PM
Steve
Steve - avatar
+ 2
Brian I didn't know this! Thanks!!
1st Oct 2021, 5:14 AM
Harsha S
Harsha S - avatar
+ 1
Simon Sauter thank you
1st Oct 2021, 5:24 AM
Harsha S
Harsha S - avatar
- 4
C programming
2nd Oct 2021, 9:51 AM
Jagadesh Lucky
- 6
سلام
2nd Oct 2021, 3:43 PM
زانکو محمودنژاد
- 7
Hii
1st Oct 2021, 3:11 PM
Seebu Rajput
- 7
Hi
2nd Oct 2021, 9:50 AM
Jagadesh Lucky