Python (Fall 2026) Module 9-2: Creating a Sorting Algorithm
Overview
Bill Moseley explains algorithms as repeatable step-by-step procedures, then develops a selection sort by repeatedly finding the smallest remaining value and moving it to a separate list. His Python example scans a list, tracks the minimum with `intLow`, appends it to `liSorted`, and removes it from the original list until empty; the approach handles duplicates and generalizes across list sizes, provided the initial minimum value is sufficiently large.
Key takeaways
- An algorithm is a repeatable step-by-step procedure that can solve many instances of the same problem, as illustrated by multi-digit multiplication.
- Selection sort finds the smallest value still unsorted, transfers it to the output, and repeats until no values remain.
- In the Python implementation, `intLow` tracks the current minimum during a scan of `liNum`, while `liSorted` collects values in ascending order.
- `list.remove(value)` is appropriate when deleting the selected minimum by its value; `list.pop()` instead removes an item by index.
- The implementation can sort duplicate values, but its initial `intLow` sentinel must be larger than every input value or replaced with a safer approach.
Chapters
- Multi-digit multiplication follows the same sequence of steps regardless of which numbers are being multiplied.
- A procedure that works across different inputs is an algorithm: a step-by-step method for solving a problem.
- Many simple operations can make a large calculation error-prone, illustrating why consistent procedures matter.
- To sort number cards in ascending order, scan the remaining cards, keep track of the lowest value, and move it to a separate stack.
- Repeat the scan and transfer until every card has been removed from the unsorted group.
- The same selection process applies when the card values are stored in a Python list rather than laid out physically.
- Use `while len(liNum) > 0` to repeat the selection process until the original list is empty.
- Scan each `intNum` in `liNum`, updating `intLow` whenever a smaller value is found; initialize `intLow` above all possible values, such as 1,000 for the example's range.
- Append the minimum with `liSorted.append(intLow)` and remove that value with `liNum.remove(intLow)`; `remove` takes a value, unlike `pop`, which uses an index.
- The demonstrated sort handles duplicate values and different list sizes; choosing a sufficiently large initial minimum avoids limiting valid inputs to numbers below 1,000.
Transcript excerpt
0:01 So an algorithm is something we use all the time. Now if I ask you to solve this math problem, which is, you know, pretty basic. It's probably like third or fourth grade math or whatever. We all have a method for solving this type of math problem. Correct? So what's the first thing I do when I want to solve this problem? The very first step. Eight. One times eight. There we go. I knew somebody knew. So I go one times eight. I write it down here. Maybe I carry, and not in this case. I do three times eight. Write the answer. And then I go to here. Right? And no matter what numbers I show you here, you're going to use those same steps to solve that same problem. So, it's an algorithm for solving
0:56 multiple digit multiplication. Would you all agree that that same algorithm would allow me to, in theory, solve this problem as well? I can use that same method, right? Four, seven, and I, now it's going to be ugly, right? Nobody wants to solve this problem because you've got like, I don't know, what is that, like 15, you'd have like 15 lines of addition down here when you're done. The odds of us getting the answer correct are probably pretty slim, because even though each one of these is a really simple multiplication and addition problem, there's so much of it that we probably are going to make one tiny little error somewhere. And that's the joy of mathematics, right? So this is the same algorithm as this. So algorithms allow us to understand different specific problems in the same general way.
1:55 A lot of times now in computer science we create algorithms for all kinds of things. An algorithm is just a simple, like I said, a step-by-step procedure for solving a problem.
This is the start of the transcript. Save this video free for the whole transcript, searchable and time-coded.
In this course
Related notes
Summary, takeaways, and chapters were generated by AI from the video's transcript and may contain errors. The video belongs to its creator, Bill Moseley, Ph.D. - Computer Science Educator.