+ 1
Insertion sort using recursion
def insertion_sort(seq): isort(seq,len(seq)) def isort(seq,k): if k>1: isort(seq,k-1) insert(seq,k-1) def insert(seq,k): print(k) pos=k while pos>0 and seq[pos]<seq[pos-1]: (seq[pos],seq[pos-1])=(seq[pos-1],seq[pos]) pos-=1 print(seq) seq=[9,8,7,6,5,4,3,2,1] insertion_sort(seq) print(seq) In this python code isort is called recursively my doubt is how the recursive function is stored and executed in stack and how many times insert function inside isort function is called.
1 ответ
0
isort is called recursively until index 1, and it is getting swapped with previous index value until it is greater than value at that index...
So len(seq) =k =>9 it is recursively calling until k=1.
And in insert function, value getting positioned correct place..