Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Algorithmic Python

Algorithmic Python

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

348cfd39887a39d65a7a99cd849d1d12?s=128

Jessica Greene

November 09, 2019
Tweet

Transcript

  1. None
  2. None
  3. grokking algorithms

  4. 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 } } } }
  5. None
  6. 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
  7. None
  8. 0(1) 0(log n) 0(n) 0(n log n) 0(n^2) 0(2^n) 0(nl)

  9. None
  10. None
  11. https://medium.com/@george.seif94/a-tour-of-the-top-5-sorting-algorithms-with-python-code-43ea9aa02889

  12. 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]
  13. None
  14. https://hackernoon.com/timsort-the-fastest-sorting-algorithm-youve-never-heard-of-36b28417f399

  15. 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
  16. None
  17. https://medium.com/@george.seif94/a-tour-of-the-top-5-sorting-algorithms-with-python-code-43ea9aa02889

  18. 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)
  19. 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)
  20. 2

  21. None
  22. https://medium.com/@george.seif94/a-tour-of-the-top-5-sorting-algorithms-with-python-code-43ea9aa02889

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

    len(array) - 1 return quick_sort_recursion(array, begin, end)
  24. 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)
  25. None
  26. None
  27. None
  28. https://www.manning.com/books/grokk ing-algorithms https://www.codecademy.com/ https://www.khanacademy.org/ https://hackernoon.com/timsort-the -fastest-sorting-algorithm-youve-ne ver-heard-of-36b28417f399 https://medium.com/@george.seif94/a -tour-of-the-top-5-sorting-algorithms-w ith-python-code-43ea9aa02889

  29. None