8.14

Lecture 20: Insertion sort🔗

You don’t need to submit anything for this lecture. But we strongly encourage you to submit it as soon as possible using Handin as assignment lecture20, to practice and get automatic feedback.

Exercise 1. To review what we’ve learned, let’s design a function our-sort to sort a list of numbers into ascending order. Do steps 1–4 of the design recipe: write the template meticulously (named our-sort), but don’t fill it in (so when you run your program, your tests should fail with the error expected a finished expression, but found a template).

Exercise 2. We wish we could just fill in the template by replacing the dots with the name of a helper function. Design the function insert which takes a Number and a ListOfNumbers and puts the number in the correct place in the list, assuming the list is already sorted. The new number should go before all the numbers bigger than it, and after all the numbers smaller than it. Don’t use the built-in sort function.