Solution: Merge and Bubble sort
examples/functions/bubble_sort.py
def bubble_sort(*values): values = list(values) for ix in range(len(values)-1): for jx in range(len(values)-1-ix): if values[jx] > values[jx+1]: values[jx], values[jx+1] = values[jx+1], values[jx] return values print(bubble_sort(1, 2, 3)) print(bubble_sort(3, 2, 1)) print(bubble_sort(10, 9, 8, 7, 6, 5, 4, 3, 2, 1))
examples/functions/iterative_bubble_sort.py
def iterative_bubble_sort(data): data = data[:] for end in (range(len(data)-1, 0, -1)): for i in range(end): if data[i] < data[i+1]: data[i], data[i+1] = data[i+1], data[i] return data old = [1, 5, 2, 4, 8] new = iterative_bubble_sort(old) print(old) print(new)
examples/functions/recursive_bubble_sort.py
def recursive_bubble_sort(data): data = data[:] if len(data) == 1: return data last = data.pop() sorted_data = recursive_bubble_sort(data) for i in range(len(sorted_data)): if last > sorted_data[i]: sorted_data.insert(i, last) break else: sorted_data.append(last) return sorted_data old = [1, 5, 2, 4, 8] new = recursive_bubble_sort(old) print(old) print(new)