经典排序算法代码示例
本文包含经典的排序算法:插入排序,选择排序,冒泡排序,希尔排序,快排,归并排序,堆排序以及简单的二分查找方法。
package SortSet;
/**
* 七种排序总结 2011-10-20
*
* @author franciscolv
*
*/
public class SortDemo {
public static void main(String[] args) {
int[] a = { 11, 1, 3, 4, 5, 2, 9, 8, 12 };
// insertSort(a);
// bubleSort(a);
// quickSort(a,0,a.length-1);
// print(a);
// mergeSort(a, 0, a.length - 1,new int[a.length]);
// print(a);
// heapSort(a);
// shellSort(a);
// selectSort(a);
// System.out.println(b_search(a,0,4,6));
}
/**
* 选择排序 选择最小的元素放到前面来 o(n*n)
*
* @param a
*/
public static void selectSort(int[] a) {
print(a);
for (int i = 0; i < a.length; i++) {
int index = i;
for (int j = i + 1; j < a.length; j++) {
if (a[j] < a[index]) {
index = j;
}
}
if (index != i) {
int tmp = a[i];
a[i] = a[index];
a[index] = tmp;
}
}
print(a);
}
/**
* o(n*n) 插入排序对每个元素,从前往后找到最合适的位置插入 稳定
*
* @param a
*/
public static void insertSort(int[] a) {
for (int i = 1; i < a.length; i++) {
int t = a[i];
for (int j = 0; j < i; j++) {
if (a[j] > a[i]) {
for (int k = i; k > j; k--)
a[k] = a[k - 1];
a[j] = t;
}
}
}
print(a);
}
/**
* 冒泡排序 把最大元素往后移动 稳定 o(n*n)
*
* @param a
*/
public static void bubleSort(int[] a) {
for (int i = 0; i < a.length - 1; i++) {
for (int j = 0; j < a.length - i - 1; j++) {
if (a[j] > a[j + 1]) {
int tmp = a[j + 1];
a[j + 1] = a[j];
a[j] = tmp;
}
}
}
print(a);
}
/**
* 希尔排序 步长递减 把小元素前移
*
* @param a
*/
public static void shellSort(int[] a) {
print(a);
for (int i = a.length / 2; i > 2; i /= 2) {
for (int j = 0; j < i; j++)
insertSort(a, j, i);
}
insertSort(a, 0, 1);
print(a);
}
private static void insertSort(int[] a, int from, int incr) {
int tmp;
for (int s = from + incr; s < a.length; s += incr) {
for (int j = s; (j >= incr) && (a[j] < a[j - incr]); j -= incr) {
tmp = a[j];
a[j] = a[j - incr];
a[j - incr] = tmp;
}
}
}
/**
* 堆排序,以最大元素构建堆,然后把最大元素后移 o(n*lgn) 不稳定
*
* @param a
*/
public static void heapSort(int[] a) {
print(a);
int n = a.length - 1;
buildHeap(a, n);
for (int i = n - 1; i >= 0; i--) {
int t = a[i + 1];
a[i + 1] = a[0];
a[0] = t;
buildHeap(a, i);
}
print(a);
}
private static void buildHeap(int[] a, int n) {
for (int i = n / 2; i >= 0; i--)
adjust(a, i, n);
}
private static void adjust(int[] a, int i, int n) {
int t = a[i];
int j = 2 * i + 1;
while (j <= n) {
if (j < n && a[j] < a[j + 1])
j++;
if (a[i] >= a[j])
break;
a[i] = a[j];
i = j;
j = 2 * j + 1;
}
a[(j - 1) / 2] = t;
}
/**
* 合并排序,将元素复制到辅助数组然后重亲拷贝回来 稳定 o(nlgn) o(n)空间
*
* @param a
* @param l
* @param r
*/
public static void mergeSort(int[] a, int l, int r, int[] t) {
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(a, l, m, t);
mergeSort(a, m + 1, r, t);
merge(a, l, m, r, t);
}
}
private static void merge(int[] a, int l, int m, int r, int[] t) {
int i = l, j = m + 1, k = 0;
while (i <= m && j <= r) {
if (a[i] <= a[j])
t[k++] = a[i++];
else
t[k++] = a[j++];
}
while (i <= m)
t[k++] = a[i++];
while (j <= r)
t[k++] = a[j++];
k = 0;
while (l + k <= r) {
a[l + k] = t[k];
k++;
}
}
/**
* 快排 注意标志的选取 o(nlgn)
*
* @param a
* @param l
* @param r
*/
public static void quickSort(int[] a, int l, int r) {
if (l >= r)
return;
int m = partition(a, l, r);
quickSort(a, l, m - 1);
quickSort(a, m + 1, r);
}
private static int partition(int a[], int l, int r) {
int x = a[l];
int i = l, j = r;
while (i < j) {
while (i < j && a[j] >= x)
j--;
if (i < j)
a[i++] = a[j];
while (i < j && a[i] <= x)
i++;
if (i < j)
a[j--] = a[i];
}
a[i] = x;
return i;
}
/**
* 有序数组的二分查找o(lgn)
*
* @param a
* @param l
* @param r
* @param k
* @return
*/
public static int b_search(int[] a, int l, int r, int k) {
if (l >= r)
return k == a[l] ? l : -1;
if (k < a[l] || k > a[r])
return -1;
int m = l + (r - l) / 2;
if (k <= a[m])
return b_search(a, l, m, k);
else
return b_search(a, m + 1, r, k);
}
private static void print(int[] a) {
for (int i : a)
System.out.print(i + " ");
System.out.println();
}
}
转载请注明作者与出处:franciscolv http://www.cnblogs.com/franciscolv/archive/2011/11/20/2255812.html