In the setup we created a solution with two projects. The purpose of these two projects was for a Sorting Kata.
In Linq OrderBy we added a Bookshelf class to hold our sorting methods.
In List Sort we used the built-in sort method of a list. This required us to implement IComparable in the Book class.
In Bubble Sort we overloaded the < and > operators so that books could be sorted through a simple sort.
Here is our Book class.
Now we are going to write our own Insertion Sort method.
We write a failing test first.
We use CTRL+. to add the InsertionSort method that accepts the list of books. Our goal is for the method to return a sorted list. Then we can assert that the correct book appears first.
Running the test causes the following error:
1 test failed: System.NotImplementedException
Hurray, the test failed. We use the Red, Green, Refactor pattern from Kent Beck's book, Test Driven Design By Example.
The problem is, it failed for the wrong reason. We want to see a failure because the books are not sorted. Let's take a look at the Bookshelf class.
Well, that explains our "NotImplementedException".
So, what is it going to take to get this test to fail for the right reason? Let's take a look at Uncle Bob's Transformation Priority Premise. The first transformation we should try is "nil". Can we get the test to fail by doing nothing? No, our code will not compile unless we return something. The next step is a constant. Let's just return the books list as is.
1 test failed Expected: Partnoy, Frank. Wait: The Art and Science of Delay But was: Watt, Andrew. Beginning Regular Expressions
In Linq OrderBy we overrode the Book.ToString() to get the author and title.
We saw a failing test (Red), and we made sure the message was helpful. Now we can make it green.
The Insertion Sort algorithm defined on Sorting-Algorithms.com is:
Let's translate this into C#. Instead of a, i, k, and n, I'm going to give the variables longer names.
Let me explain why this code differs from the algorithm.
I subtracted 1 from the initial bookIndex and allBooksIndex values so that they are zero based.
The first book's index is zero. We will be comparing a book to the book that precedes it. So we initialize the "bookIndex" variable to be 1 so that we can compare the second book on the shelf to the first book. We keep looping through the books as long as bookIndex is less than the number of books. We add 1 to bookIndex to keep moving through the list.
Our inner loop starts with the book that is defined in the outer loop. If you are looking at the bookshelf. You would grab the second book and compare it to the first book. If the book you are holding precedes the book that is next to it, swap them. I reversed the > symbol in my loop from the one defined in the algorithm because I want the books in ascending order.
We swap books by taking the book off the shelf and putting it in a "swapBook" variable. Then we take the book that is next to it and insert that book in the swapped book's spot. Then we insert the book into that empty slot.
When we run the test we see:
1 test passing
I recommend setting a breakpoint and watching the list of books change order.
Please contact me about this Kata. I would love to get your feedback.
Other sorting methods: