Archive

Archive for the ‘Java’ Category

Bubble Sort algorithm developed with TDD. Code Kata.

16/05/2011 2 comments

After I’ve been on a three day course with Uncle Bob in Copehangen, I got inspired by the code kata concept. And now want to do Kata from time to time to train coding.

I’ve wanted to start with something simple so I chose to do a sort algorithm. I was trying to make a video out of it, but got stuck with quicktime. Hoping to get it to work in the future. So for now you will have to settle with the testcases and code. I’m trying to use the guidelines from Clean Code so no comments will be in the code, functions and variable names should reveal their purpose.¬† How the code works should be visible from the Testcases.

Test cases.


package Sort;

import junit.framework.Assert;
import org.junit.Test;

import java.util.Arrays;

public class SorterTest {
  @Test
  public void SortEmptyList_ShouldReturnEmptyList() {
    Assert.assertTrue(Arrays.equals(new int[]{},
                        Sorter.Sort(new int[]{})));
 }

 @Test
 public void SortListWithOneElement_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{1},
                       Sorter.Sort(new int[]{1})));
 }

 @Test
 public void SortListWithTwoElementsInCorrectOrder_ShouldReturnSameList() {
   Assert.assertTrue(Arrays.equals(new int[]{1, 2},
                       Sorter.Sort(new int[]{1, 2})));
 }

 @Test
 public void SortListWithTwoElementsInReverseOrder_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{1, 2},
                       Sorter.Sort(new int[]{2, 1})));
 }

 @Test
 public void SortListWithSameTwoElementsr_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{2, 2},
                       Sorter.Sort(new int[]{2, 2})));
 }

 @Test
 public void SortListWithThreeElementsInCorrectOrder_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{1, 2, 3},
                       Sorter.Sort(new int[]{1, 2, 3})));
 }

 @Test
 public void SortListWithThreeElementsFirstTwoSwapped_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{1, 2, 3},
                       Sorter.Sort(new int[]{2, 1, 3})));
 }

 @Test
 public void SortListWithThreeElementslastTwoSwapped_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{1, 2, 3},
                       Sorter.Sort(new int[]{1, 3, 2})));
 }

 @Test
 public void SortListWithThreeElementslReverseOrder_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{1, 2, 3},
                       Sorter.Sort(new int[]{3, 2, 1})));
 }

 @Test
 public void SortListWithNElementaRandomOrder_ShouldReturnSortedList() {
   Assert.assertTrue(Arrays.equals(new int[]{1, 2, 3, 4, 5, 6, 6, 7, 8},
                       Sorter.Sort(new int[]{3, 2, 7, 6, 4, 8, 6, 1, 5})));
 }
}

And now to the refactored code.


package Sort;

public class Sorter {
  public static int[] Sort(int[] unSortedList) {
    if (unSortedList.length > 1)
      SortList(unSortedList);
    return unSortedList;
 }

 private static void SortList(int[] unSortedList) {
   int unsortedLength = unSortedList.length;
   while (unsortedLength > 0) {
     PlacedLargestElementAtEndOfList(unSortedList);
     unsortedLength--;
   }
 }

 private static void PlacedLargestElementAtEndOfList(int[] unSortedList) {
   for (int j = 0; j < i.length - 1; j++)
     SortElements(unSortedList, j);
 }

 private static void SortElements(int[] unSortedList, int j) {
   if (unSortedList[j] > unSortedList[j + 1])
     SwapElements(unSortedList, j);
 }

 private static void SwapElements(int[] unSortedList, int j) {
   int backupelement = unSortedList[j];
   unSortedList[j] = unSortedList[j + 1];
   unSortedList[j + 1] = backupelement;
 }
}
Advertisements
Categories: Java Tags: , ,