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.
Here is our Book class.
Now we are going to write our own Bubble Sort method.
We write a failing test first.
We use CTRL+. to add the BubbleSort 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 Bubble Sort algorithm defined on Sorting-Algorithms.com is:
Let's translate this into C#. Instead of a, i, j, and n, I'm going to give the variables longer names.
Let me explain this code.
- "keepSwapping" is a Boolean that is initially set to true. It tells us if we had to swap any books. Inside the loop we set it to false and only set it back to true if we performed a swap. If we loop through all of the books and never perform a swap, we can stop. We simply exit out and stop trying to sort an already sorted list.
- "swapBook" is a Book that we are going to swap. You can imagine it as the book that you are holding in your hand as you are rearranging the bookshelf.
- "bookCount" is the number of books on the shelf. We get this from the count property of the list. We have to subtract one because our list is zero based. If we have three books on the shelf our count is three but we want the index to stop at 2. Our books are numbered 0, 1, 2.
Now we begin the outer loop for each book. The first book's index is 0. So we initialize the "bookIndex" variable to be 0. We keep looping through the books as long as bookIndex is less than or equal to bookCount and we performed a swap at some point. Then we add 1 to bookIndex to keep moving through the list.
The first thing we do inside the outer loop is set "keepSwapping" variable to false. It's up to the inner loop to change it back to true.
Our inner loop starts with the last book on the shelf. It keeps looping to the last book we sorted. If you are looking at the book shelf. You would grab the last book and compare it to the book that sits next to it. If the book you are holding is greater than the book that is next to it, swap them. The > symbol in this case should read as "precedes". If the book you are holding precedes the book next to it, swap them.
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 put that book in the swapped book's spot. Then we put the book into that empty slot.
Finally, we set the keepSwapping Boolean back to true to let the outer loop know that it needs to keep going.
That was a wonderful explanation of code that does not compile.
Why won't our code compile? The error message reads:
Operator '>' cannot be applied to operands of type 'BookSorting.Book' and ' BookSorting.Book'
Overloading the operators is really simple because we know how to compare two books. Just add these two methods to the Book class.
The program will not compile unless you implement both the greater than and the less than operators at the same time.
I had to get out a piece of paper to understand the order of the parameters and the direction of the greater than and less than symbol.
When we use the > operator we have two books. It's Partnoy vs. Watt. Partnoy precedes Watt. If we look at the documentation of the ComareTo method we read:
|Less than zero||This instance precedes obj in the sort order.|
|Zero||This instance occurs in the same position in the sort order as obj.|
|Greater than zero||This instance follows obj in the sort order.|
If we compare Partnoy to Watt we get back less than 1 because Partnoy precedes Watt. That means Partnoy > Watt. A negative number means the book comes before the other book.
If we compare Watt to Partnoy we get back more than 1 because Watt follows Partnoy. That means Watt < Partnoy. A positive number means the book comes after the other book.
When we run the test we see:
1 test passing
To get Bubble Sort working we had to teach the Book class when it was greater or less than another book.
Then we run an outer loop for every book. The inner loop compares the last book to the book just before it.
Our book then "bubbles up" the bookshelf to be placed in order.
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: