A recursive function to sort a list of ints

后端 未结 4 660
一整个雨季
一整个雨季 2021-01-29 14:09

I want to define a recursive function can sort any list of ints:

def sort_l(l):
    if l==[]:
        return []
    else:
        if len(l)==1:
            retur         


        
4条回答
  •  迷失自我
    2021-01-29 14:49

    For this you would want to use merge sort. Essentially in a merge sort you recursively split the list in half until you have single elements and than build it back up in the correct order. merge sort on has a complexity of O(n log(n)) and is an extremely stable sorting method.

    Here are some good in depth explanations and visuals for merge sorting:

    • https://www.youtube.com/watch?v=vxENKlcs2Tw
    • http://www.princeton.edu/~achaney/tmve/wiki100k/docs/Merge_sort.html

提交回复
热议问题