常用排序算法的 Java 实现 - Common Sort Algorithms in Java

import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;


/**
* @name Sort
*
@author 那十无忧
* @date Feb 23, 2012 - 7:24:22 PM
*/
public abstract class Sort
{
private static final Sort[] instances;
static {
instances = new Sort[Type.values().length];
instances[Type.BubbleSort.ordinal()] = new BubbleSort();
instances[Type.SelectionSort.ordinal()] = new SelectionSort();
instances[Type.InsertionSort.ordinal()] = new InsertionSort();
instances[Type.MergeSort.ordinal()] = new MergeSort();
instances[Type.QuickSort.ordinal()] = new QuickSort();
}

public static Sort getInstance(Type type) {

// throw NullPointerException when type == null
Sort sort = instances[type.ordinal()];

if( sort == null )
throw new UnsupportedOperationException(
"Sort algorithm not supported yet : " + type);

return sort;
}

public static enum Type {
BubbleSort,
SelectionSort,
InsertionSort,
MergeSort,
QuickSort,
;
}

private Sort() {}

public final <E extends Comparable<E>> List<E> sort(Collection<E> col) {

return sort(col, new Comparator<E>() {

@Override
public int compare(E o1, E o2) {
return o1.compareTo(o2);
}
});
}

public final <E> List<E> sort(Collection<E> col, Comparator<? super E> c) {

// throw NullPointerException when col == null
@SuppressWarnings("unchecked")
E[] arr = (E[])col.toArray(new Object[col.size()]);
sort(arr, c);
return Arrays.asList(arr);
}

public final <E extends Comparable<E>> void sort(E[] arr) {

sort(arr, new Comparator<E>() {

@Override
public int compare(E o1, E o2) {
return o1.compareTo(o2);
}
});
}

public final <E> void sort(E[] arr, Comparator<? super E> c) {

if( c == null ) throw new NullPointerException();

// throw NullPointerException when arr == null
sortUtil(arr, c, 0, arr.length);
}

abstract <E> void sortUtil(
E[] arr,
Comparator<? super E> c,
int left,
int len);

private static <E> void swap(E[] arr, int a, int b) {

assert arr != null;
assert a >= 0 && a < arr.length;
assert b >= 0 && b < arr.length;

E temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}

private static <E> void swapr(E[] arr, int a, int b, int len) {

assert arr != null;
assert len >= 0;
assert a >= 0 && a < arr.length;
assert b >= 0 && b < arr.length;

for(int i=len-1; i>=0; i--)
swap(arr, a+i, b+i);
}

private static <E> void swapl(E[] arr, int a, int b, int len) {

assert arr != null;
assert len >= 0;
assert a >= 0 && a < arr.length;
assert b >= 0 && b < arr.length;

for(int i=0; i<len; i++)
swap(arr, a+i, b+i);
}

private static class BubbleSort extends Sort {

@Override
<E> void sortUtil(E[] arr, Comparator<? super E> c, int left, int len) {

for(int n = left + len; n > left; ) {
int newn = left;
for(int i = left + 1; i < n; i++) {
if( c.compare(arr[i-1], arr[i]) > 0 ) {
swap(arr, i-1, i);
newn = i;
}
}
n = newn;
}
}
}

private static class SelectionSort extends Sort {

@Override
<E> void sortUtil(E[] arr, Comparator<? super E> c, int left, int len) {

for(int i = left; i < left + len; i++) {
int min = i;
for(int j = i + 1; j < left + len; j++) {
if( c.compare(arr[j], arr[min]) < 0 )
min = j;
}
swap(arr, i, min);
}
}
}

private static class InsertionSort extends Sort {

@Override
<E> void sortUtil(E[] arr, Comparator<? super E> c, int left, int len) {

int limit = left + len;
for(int i = left + 1; i < limit; i++) {
E temp = arr[i];
int j = i;
while(j > 0 && c.compare(arr[j-1], temp) > 0) {
arr[j] = arr[j-1];
j--;
}
arr[j] = temp;
}
}
}

private static class MergeSort extends Sort {

@Override
<E> void sortUtil(E[] arr, Comparator<? super E> c, int left, int len) {
Arrays.sort(arr, c);
}
}

private final static class QuickSort extends Sort {

@Override
<E> void sortUtil(E[] arr, Comparator<? super E> c, int left, int len) {

if( len < 2 ) return;

if( len == 2 ) {
if( c.compare(arr[left], arr[left+1]) > 0 )
swap(arr, left, left+1);
return;
}

// pivot === arr[left]
int right = left + len - 1;
int pL = 1, pR = 0, iL = left + pL, iR = right - pR, compare;
while( iL <= iR ) {
while( iL <= iR && (compare = c.compare(arr[iL], arr[left])) <= 0 ) {
if( compare == 0 ) {
swap(arr, iL, left + pL);
pL++;
}
iL++;
}
while( iL <= iR && (compare = c.compare(arr[iR], arr[left])) >= 0 ) {
if( compare == 0 ) {
swap(arr, iR, right - pR);
pR++;
}
iR--;
}
if( iL < iR ) {
swap(arr, iL, iR);
iL++;
iR--;
}
}
swapr(arr, left, iL-pL, pL);
if( iL + pR <= right && pR > 0 )
swapl(arr, iL, right-pR+1, pR);

sortUtil(arr, c, left, iL-left-pL);
if( iL + pR < right )
sortUtil(arr, c, iL+pR, right-iL-pR+1);
}
}
}

你可能感兴趣的:(常用排序算法的 Java 实现 - Common Sort Algorithms in Java)