previous | start | next

Sorting by Comparison of Elements

Every sorting algorithm that sorts by comparing elements with each other can be represented by a decision tree.

For example, consider the bubble sort method:

public class Bubble
{
  public static <E extends Comparable<E>> void sort(E[] a)
  {
    for(int len = a.length; len > 1; len--) {
      for(int i = 0; i < len - 1; i++) {
        if (a[i].compareTo(a[i + 1]) > 0) {
          E tmp = a[i];
          a[i] = a[i + 1];
          a[i + 1] = tmp;
        }
      }
    }
}


previous | start | next