Jessica Greene
November 09, 2019
50

# Algorithmic Python

Talk given as part of Python Pizza Hamburg 2019 and PyLadies Berlin December 2019 meetup.

## Jessica Greene

November 09, 2019

## Transcript

2. ### https://github.com/wwgberlin/algorithms //for each neighboring pair of integers in the slice

//swap if left is larger than right //stop when nothing was swapped //complexity O(n2) func bubbleSort(s []int) { swapped := t￼rue for swapped { swapped = false for i := 1; i < len(s); i++ { if s[i-1] > s[i] { temp := s[i] s[i] = s[i-1] s[i-1] = temp swapped = true } } } }
3. ### Language Syntax is simple to learn so you can focus

on the algorithms However it is a little harder to see what is happening beneath the suﬃce. Limited to how much optimisation you can do by Python itself

6. ### def bubble_sort(arr): iteration_count = 0 for i in range (len(arr)):

for idx in range(len(arr)-i-1): iteration_count += 1 if arr[idx] > arr[idx+1]: arr[idx], arr[idx+1] = arr[idx+1], arr[idx]

8. ### def insertion_sort(arr): for i in range(len(arr)): cursor = arr[i] pos

= i while pos > 0 and arr[pos - 1] > cursor: # Swap the number down the list arr[pos] = arr[pos - 1] pos = pos - 1 # Break and do the final swap arr[pos] = cursor return arr

10. ### def merge_sort(items): if len(items) <= 1: return items middle_index =

len(items) // 2 left_split = items[:middle_index] right_split = items[middle_index:] left_sorted = merge_sort(left_split) right_sorted = merge_sort(right_split) return merge(left_sorted, right_sorted)
11. ### def merge(left, right): result =[] while (left and right): if

left[0] < right[0]: result.append(left[0]) left.pop(0) else: result.append(right[0]) right.pop(0)

14. ### def quick_sort(array, begin=0, end=None): if end is None: end =

len(array) - 1 return quick_sort_recursion(array, begin, end)
15. ### def partition(array, begin, end): pivot_idx = begin for i in

xrange(begin+1, end+1): if array[i] <= array[begin]: pivot_idx += 1 array[i], array[pivot_idx] = array[pivot_idx], array[i] array[pivot_idx], array[begin] = array[begin], array[pivot_idx] return pivot_idx def quick_sort_recursion(array, begin, end): if begin >= end: return pivot_idx = partition(array, begin, end) quick_sort_recursion(array, begin, pivot_idx-1) quick_sort_recursion(array, pivot_idx+1, end)