常见排序算法实现

package sequence;

import java.util.ArrayList;

public class CommonSequence<T extends Comparable<T>> {
	/***
	 * 选择排序
	 * 
	 * @param a
	 */
	public void select(T a[]) {
		for (int i = 0; i < a.length - 1; i++) {
			int min = i;
			for (int j = i; j < a.length; j++) {
				if (a[j].compareTo(a[min]) < 0) {
					min = j;
				}
			}
			T t = a[i];
			a[i] = a[min];
			a[min] = t;
		}
	}

	/***
	 * 冒泡排序
	 * 
	 * @param a
	 */
	public void bubble(T a[]) {
		for (int i = 0; i < a.length - 1; i++) {
			for (int j = 0; j < a.length - i - 1; j++) {
				if (a[j].compareTo(a[j + 1]) < 0) {
					T t = a[j];
					a[j] = a[j + 1];
					a[j + 1] = t;
				}
			}
		}
		print(a);
	}

	/***
	 * 快速排序
	 * 
	 * @param a
	 * @param left
	 * @param right
	 */
	public void quick(T a[], int left, int right) {
		if (left >= right) {
			return;
		}
		T temp = a[left];
		int i = left;
		int j = right;
		while (i < j) {
			while (a[j].compareTo(temp) >= 0 && j > i) {
				j--;
			}
			while (a[i].compareTo(temp) <= 0 && i < j) {
				i++;
			}
			if (i < j) {
				T t = a[i];
				a[i] = a[j];
				a[j] = t;
			}
		}
		a[left] = a[i];
		a[i] = temp;
		quick(a, left, i - 1);
		quick(a, i + 1, right);
	}

	/***
	 * 计数排序
	 * 
	 * @param a
	 * @return
	 */
	public int[] count(int a[]) {
		int newA[] = new int[a.length];
		int max = a[0];
		int min = a[0];
		for (int i = 0; i < a.length; i++) {
			if (a[i] > max) {
				max = a[i];
			}
			if (a[i] < min) {
				min = a[i];
			}
		}
		int k = max - min + 1;
		int c[] = new int[k];
		for (int i = 0; i < a.length; i++) {
			c[a[i] - min]++;
		}
		for (int i = 1; i < k; i++) {
			c[i] += c[i - 1];
		}
		for (int i = 0; i < a.length; i++) {
			newA[c[a[i] - min] - 1] = a[i];
			c[a[i] - min]--;
		}
		return newA;
	}

	/***
	 * 基数排序
	 * 
	 * @param a
	 */
	public void radix(int a[]) {
		int maxKey = 0;
		int max = a[0];
		for (int i = 0; i < a.length; i++) {
			if (a[i] > max) {
				max = a[i];
			}
		}
		maxKey = (int) Math.log10(max) + 1;
		int index = 1;
		int pail[][] = new int[10][a.length];
		int pailC[] = new int[10];
		while (index <= maxKey) {
			for (int i = 0; i < a.length; i++) {
				int p = (a[i] / (int) Math.pow(10, index - 1)) % 10;
				pail[p][pailC[p]] = a[i];
				pailC[p]++;
			}
			int m = 0;
			for (int i = 0; i < 10; i++) {
				if (pailC[i] > 0) {
					for (int j = 0; j < pailC[i]; j++) {
						a[m] = pail[i][j];
						m++;
					}
					pailC[i] = 0;
				}
			}
			index++;
		}
	}

	/***
	 * 插入排序
	 * 
	 * @param a
	 */
	public void insert(T a[]) {
		for (int i = 1; i < a.length; i++) {
			T t = a[i];
			int j = 0;
			for (j = i - 1; j >= 0; j--) {
				if (t.compareTo(a[j]) < 0) {
					a[j + 1] = a[j];
				} else {
					break;
				}
			}
			a[j + 1] = t;
		}
	}

	/***
	 * 合并数据
	 * 
	 * @param a
	 * @param s
	 * @param m
	 * @param e
	 */
	private void mergeIn(T a[], int s, int m, int e) {
		ArrayList<T> tmp = new ArrayList<>(e - s + 1);
		int i = s;
		int j = m;
		int k = 0;
		while (i < m && j <= e) {
			if (a[i].compareTo(a[j]) < 0) {
				tmp.add(k, a[i]);
				i++;
				k++;
			} else {
				tmp.add(k, a[j]);
				j++;
				k++;
			}
		}
		if (i < m) {
			for (; i < m; i++) {
				tmp.add(k, a[i]);
				k++;
			}
		}
		if (j <= e) {
			for (; j <= e; j++) {
				tmp.add(k, a[j]);
				k++;
			}
		}
		for (k = 0; k < tmp.size(); k++) {
			a[s + k] = tmp.get(k);
		}
	}

	/***
	 * 归并排序
	 * 
	 * @param a
	 * @param s
	 * @param l
	 */
	public void merge(T a[], int s, int l) {
		int c = a.length / (l * 2);
		int surplus = a.length % (l * 2);
		if (c == 0) {
			return;
		}
		for (int i = 0; i < c; i++) {
			int ss = i * l * 2;
			mergeIn(a, ss, ss + l, ss + l * 2 - 1);
		}
		if (surplus != 0) {
			mergeIn(a, a.length - surplus - 2 * l, a.length - surplus, a.length - 1);
		}
		merge(a, s, 2 * l);
	}

	private void buildHead(T a[]) {
		for (int i = (a.length - 2) / 2; i >= 0; i--) {
			adjustHead(a, i, a.length);
		}
	}

	private void adjustHead(T a[], int r, int size) {
		int leftChild = 2 * r + 1;
		int rightChild = 2 * r + 2;
		int max = r;
		if (leftChild < size && a[leftChild].compareTo(a[max]) > 0) {
			max = leftChild;
		}
		if (rightChild < size && a[rightChild].compareTo(a[max]) > 0) {
			max = rightChild;
		}
		if (max != r) {
			T t = a[r];
			a[r] = a[max];
			a[max] = t;
			adjustHead(a, max, size);
		}
	}

	/***
	 * 堆排序
	 * 
	 * @param a
	 */
	public void head(T a[]) {
		buildHead(a);
		for (int i = a.length - 1; i >= 0; i--) {
			T t = a[0];
			a[0] = a[i];
			a[i] = t;
			adjustHead(a, 0, i);
		}
	}

	public void print(T a[]) {
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}

	public void print(int a[]) {
		for (int i = 0; i < a.length; i++) {
			System.out.println(a[i]);
		}
	}

	public static void main(String[] args) {
		Integer a[] = { 0, 2, -1, 2123, 22, 43, 5, 9889, 83, 37323, 232, 32324, 4354, 55656, 328, 2, 334, 54, 654, 6, 4,
				4242, 3 };
		// String a[] = { "dasdi", "da", "qei", "diij", "dai9" };
		// new CommonSequence<String>().bubble(a);
		// CommonSequence<String> o = new CommonSequence<String>();
		CommonSequence<Integer> o = new CommonSequence<Integer>();
		// o.quick(a, 0, a.length - 1);
		// o.select(a);
		// int newA[] = o.count(a);
		// o.radix(a);
		// o.insert(a);
		// o.merge(a, 0, 1);
		o.head(a);
		o.print(a);
	}

}
点赞