java五種排序算法匯總
java的排序算法工具有哪些?這個(gè)工具類簡單明了地總結(jié)了java的快速排序,希爾排序,插入排序,堆排序,歸并排序五種排序算法,代碼中并沒有對(duì)這幾種排序算法的一個(gè)說明,關(guān)于思想部分希望在自行查閱相關(guān)說明,這里只是對(duì)這幾種算法進(jìn)行一個(gè)概括,以供大家使用。
private static
int j; // 記錄第一個(gè)比tmp小的.元素的后邊一位的位置
for (int p = left; p <= right; p++) {
AnyType tmp = a[p];
for (j = p; j > left && tmp.compareTo(a[j - 1]) < 0; j--) {
a[j] = a[j - 1];
}
a[j] = tmp;
}
}
public static
int j;
for (int gap = arr.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < arr.length; i++) {
AnyType tmp = arr[i];
for (j = i; j >= gap && tmp.compareTo(arr[j - gap]) < 0; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = tmp;
}
}
}
private static int leftChild(int i) {
return i * 2 + 1;
}
private static
AnyType tmp = arr[i];
for (int child; (child = leftChild(i)) < size; i = child) {
if (child != size - 1 && arr[child].compareTo(arr[child + 1]) < 0) {
child++;
}
if (tmp.compareTo(arr[child]) < 0) {
arr[i] = arr[child];
} else {
break;
}
}
arr[i] = tmp;
}
public static
for (int i = arr.length / 2; i >= 0; i--) {
perculateDown(arr, i, arr.length);
}
for (int i = arr.length - 1; i >= 0; i--) {
swapReferences(arr, 0, i);
perculateDown(arr, 0, i);
}
}
private static
AnyType tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
public static
AnyType[] tmp = ((AnyType[]) new Comparable[arr.length]);
mergeSort(arr, 0, arr.length - 1, tmp);
}
private static
if (start < end) {
int mid = (start + end) >> 1;
mergeSort(arr, start, mid, tmp);
mergeSort(arr, mid + 1, end, tmp);
merge(arr, start, mid, end, tmp);
}
}
private static
int i = start, j = mid + 1, k = start;
while (i <= mid && j <= end) {
if (arr[i].compareTo(arr[j]) < 0) {
tmp[k++] = arr[i++];
} else {
tmp[k++] = arr[j++];
}
}
while (i <= mid) {
tmp[k++] = arr[i++];
}
while (j <= end) {
tmp[k++] = arr[j++];
}
for (int m = start; m <= end; m++) {
arr[m] = tmp[m];
}
}
public static
quickSort(arr, 0, arr.length - 1);
}
private static
if (left + LENGTH_DIFF <= right) {
AnyType pivot = medium(arr, left, right);
int i = left, j = right;
while (true) {
while (arr[++i].compareTo(pivot) < 0);
while (arr[--j].compareTo(pivot) > 0);
if (i < j) {
swapReferences(arr, i, j);
} else {
break;
}
}
swapReferences(arr, i, right);
quickSort(arr, left, i - 1);
quickSort(arr, i + 1, right);
} else {
insertionSort(arr, left, right);
}
}
private static
int right) {
int center = (left + right) / 2;
if (arr[center].compareTo(arr[left]) < 0) {
swapReferences(arr, center, left);
}
if (arr[left].compareTo(arr[right]) > 0) {
swapReferences(arr, left, right);
}
if (arr[center].compareTo(arr[right]) < 0) {
swapReferences(arr, center, right);
}
return arr[right];
}
private final static int LENGTH_DIFF = 20;
}
【java五種排序算法匯總】相關(guān)文章:
java排序算法大全06-07
java算法字符組合排序06-07
常見的php排序算法11-19
Java編程中如何實(shí)現(xiàn)中文排序09-27
關(guān)于Java通用權(quán)限控制的算法10-07
常用排序算法之JavaScript實(shí)現(xiàn)代碼段10-27
Java認(rèn)證輔導(dǎo):Java實(shí)現(xiàn)二叉樹遍歷算法10-03