Algorithmic Python

Algorithmic Python

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

Jessica Greene

November 09, 2019

Annual Pro sale · Save $30

Have presentations of your own?

Share with private links, password protection, and more control over your work.

Transcript

29 slides
  1. 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 := true 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 } } } }
  2. 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 suffice. Limited to how much optimisation you can do by Python itself
  3. 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]
  4. 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
  5. 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)
  6. 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)
  7. 2

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

    len(array) - 1 return quick_sort_recursion(array, begin, end)
  9. 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)

More Decks by Jessica Greene

Other Decks in Technology