Bubble sort
public static <T extends Comparable<T>> void bubbleSort(T[] array) {
boolean swapped;
do {
swapped = false;
for (int i = 0; i < array.length - 1; i++) {
if (array[i].compareTo(array[i+1]) > 0) {
//swap the two
E temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
swapped = true;
}
}
}while (swapped);
}
- Trace: [5, 3, 1, 4]
- show i, swapped, and array contents every time you reach the end of the for loop
Merge sort code (1 + 2 of 3)
public static <T extends Comparable<T>> void mergeSort(T[] array) {
@SuppressWarnings("unchecked")
T[] aux = (T[]) new Comparable[array.length];
mergeSort(array, aux, 0, array.length - 1);
}
private static <E extends Comparable<E>> void mergeSort(T[] array,
T[] aux, int left, int right) {
if(left < right) {
int mid = (left + right) / 2;
mergeSort(array, aux, left, mid);
mergeSort(array, aux, mid + 1, right);
merge(array, aux, left, mid, right);
}
}
Merge sort code (3/3)
private static <T extends Comparable<T>> void merge(T[] array, T[] aux,
int startLeft, int mid, int endRight) {
int endLeft = mid;
int startRight = mid + 1;
//cursor indexes
int leftPos = startLeft;
int rightPos = startRight;
int auxPos = startLeft;
//merge from left or right, whichever is smaller, into aux
while (leftPos <= endLeft && rightPos <= endRight) {
if (array[leftPos].compareTo(array[rightPos]) <= 0) {
aux[auxPos++] = array[leftPos++];
}else {
aux[auxPos++] = array[rightPos++];
}
}
//at this point, all of left or all of right copied over
//so copy the remainder of other
while(leftPos <= endLeft) {
aux[auxPos++] = array[leftPos++];
}
while(rightPos <= endRight) {
aux[auxPos++] = array[rightPos++];
}
//now copy sorted aux section back into original array
for(int i = startLeft; i <= endRight; i++)
array[i] = aux[i];
}
}
//based partly on the implementation from http://www.java-tips.org/java-se-tips/java.lang/merge-sort-implementation-in-java.html
Quicksort code (for reference)
public static <T extends Comparable<? super T>> void quickSort(T[] array) {
quickSort(array, 0, array.length - 1);
}
private static <T extends Comparable<? super T>> void quickSort(T[] array,
int start, int end) {
if (start < end) {
int pivot = partition(array, start, end);
quickSort(array, start, pivot - 1);
quickSort(array, pivot + 1, end);
}
}
/**
* Picks a random pivot value from the given range and then
* sorts the remaining values into two groups: those larger
* and those smaller than the pivot. Places the pivot value
* between these groups and returns its index.
*/
private static <T extends Comparable<? super T>> int partition(T[] array,
int start, int end) {
//find pivot and swap into end position
int pivot = (int) (Math.random() * (end - start + 1)) + start;
T temp = array[pivot];
array[pivot] = array[end];
array[end] = temp;
pivot = end;
int less = start - 1;
for (int greater = start; greater < end; greater++) {
if (array[greater].compareTo(array[pivot]) < 0) {
//actually expand lesser group instead
less++;
temp = array[less];
array[less] = array[greater];
array[greater] = temp;
}
//else: just leave where it is in the greater range
}
//swap pivot into end of lesser group
less++; //actually first of greater set
temp = array[less];
array[less] = array[pivot];
array[pivot] = temp;
pivot = less;
return pivot;
}