Find the second smallest number in a list using recursion

后端 未结 6 1937
故里飘歌
故里飘歌 2021-02-19 18:08

I know there has been a question asked on this topic, but none of the answers have helped me. I don\'t need help with implementing the code, I just need help sorting through the

6条回答
  •  暖寄归人
    2021-02-19 18:41

    I can think of several ways. The slow one is to find the largest value of the given list, and then recur on the rest of the list. When the list length is 2, don't make the recursive call.

    A faster one is to find the smallest element, recur on the remainder of the list, iterating only twice. Write a routine to find the nth smallest element this way, and then wrap it in a routine that calls it with n=2.

    Is that enough of a start?

提交回复
热议问题