Skip to content

Latest commit

 

History

History
85 lines (75 loc) · 5.97 KB

Class11.md

File metadata and controls

85 lines (75 loc) · 5.97 KB

Class 11: Iterative Sorting Algorithms

Topics

Resources

Challenges

  • Implement these classic iterative sorting functions using sorting starter code:
    • is_sorted(items) - return a boolean indicating whether all items are in ascending order
    • bubble_sort(items) - swap adjacent items that are out of order, repeat until all items are sorted
    • selection_sort(items) - find minimum item in unsorted items, swap it with first unsorted item, repeat until all items are sorted
    • insertion_sort(items) - take first unsorted item, insert it in sorted order in front of items, repeat until all items are sorted
  • Run python sorting.py to test sorting algorithms on a small random sample:
    $ python sorting.py bubble_sort 10 20
    Initial items: [3, 15, 4, 7, 20, 6, 18, 11, 9, 7]
    Sorting items with bubble_sort(items)
    Sorted items:  [3, 4, 6, 7, 7, 9, 11, 15, 18, 20]
    
  • Annotate functions with complexity analysis of running time (operations) and space (memory usage)
  • Write a thorough suite of sorting unit tests to ensure your sorting algorithms are robust
    • Write tests in a way that lets you add new sorting functions without needing to write more tests
    • Include a variety of test cases that cover many different input types, orderings, distributions, and edge cases
  • Run pytest sorting_test.py to run the sorting unit tests and fix any failures

Stretch Challenges

  • Extend sorting algorithms with an order parameter to specify ascending or descending order
  • Extend sorting algorithms with a key parameter to specify a function to call on each item when making comparisons (read about Python's sorted function and how to use key functions for more information)
  • Implement an insertion sort variation using binary search to find the position to insert each item
  • Implement improved iterative sorting algorithms: cocktail shaker sort, library sort, or Shell sort
  • Annotate functions with complexity analysis of running time (operations) and space (memory usage)