What does this code do?
That principle is called
Recursion occurs when the definition of a concept or process depends on a simpler version of itself
Making sourdough requires sourdough.
Recursion in computer science means that a function calls itself.
That requires
Using recursion, you can implement Quicksort,
an
efficient sorting algorithm:
def quicksort(lst):
if len(lst) == 0: # the breaking condition
return lst
pivot = lst[0]
pivots = [x for x in lst if x == pivot]
# here, the recursion happens
# but with "simpler" (i.e. shorter)
# versions of the list
small = quicksort([x for x in lst if x < pivot])
large = quicksort([x for x in lst if x > pivot])
return small + pivots + large
quicksort([5,4,9,2,6,3,8,7,1,0])
mirco.schoenfeld@uni-bayreuth.de