常用Java排序算法詳解。本站提示廣大學習愛好者:(常用Java排序算法詳解)文章只能為提供參考,不一定能成為您想要的結果。以下是常用Java排序算法詳解正文
一、選擇排序(SelectSort)
根本原理:關於給定的一組記載,經過第一輪比擬後失掉最小的記載,然後將該記載與第一個記載的地位停止交流;接著對不包括第一個記載以外的其他記載停止第二次比擬,失掉最小的記載並與第二個記載停止地位交流;反復該進程,直到停止比擬的記載只要一個為止。
public class SelectSort {
public static void selectSort(int[] array) {
int i;
int j;
int temp;
int flag;
for (i = 0; i < array.length; i++) {
temp = array[i];
flag = i;
for (j = i + 1; j < array.length; j++) {
if (array[j] < temp) {
temp = array[j];
flag = j;
}
}
if (flag != i) {
array[flag] = array[i];
array[i] = temp;
}
}
}
public static void main(String[] args) {
int[] a = { 5, 1, 9, 6, 7, 2, 8, 4, 3 };
selectSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
二、拔出排序(InsertSort)
根本原理:關於給定的一組數據,初始時假定第一個記載自成一個有序序列,其他記載為無序序列。接著從第二個記載開端,依照記載的大小順次將以後處置的記載拔出到其之前的有序序列中,直至最後一個記載拔出到有序序列中為止。
public class InsertSort {
public static void insertSort(int[] a) {
if (a != null) {
for (int i = 1; i < a.length; i++) {
int temp = a[i];
int j = i;
if (a[j - 1] > temp) {
while (j >= 1 && a[j - 1] > temp) {
a[j] = a[j - 1];
j--;
}
}
a[j] = temp;
}
}
}
public static void main(String[] args) {
int[] a = { 5, 1, 7, 2, 8, 4, 3, 9, 6 };
// int[] a =null;
insertSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
三、冒泡排序(BubbleSort)
根本原理:關於給定的n個記載,從第一個記載開端順次對相鄰的兩個記載停止比擬,以後面的記載大於前面的記載時,交流地位,停止一輪比擬和換位後,n個記載中的最大記載將位於第n位;然後對前(n-1)個記載停止第二輪比擬;反復該進程直到停止比擬的記載只剩下一個為止。
public class BubbleSort {
public static void bubbleSort(int array[]) {
int temp = 0;
int n = array.length;
for (int i = n - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int a[] = { 45, 1, 21, 17, 69, 99, 32 };
bubbleSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
四、歸並排序(MergeSort)
根本原理:應用遞歸與分治技術將數據序列劃分紅為越來越小的半子表,再對半子表排序,最後再用遞歸辦法將排好序的半子表兼並成為越來越大的有序序列。關於給定的一組記載(假定共有n個記載),首先將每兩個相鄰的長度為1的子序列停止歸並,失掉n/2(向上取整)個長度為2或1的有序子序列,再將其兩兩歸並,重復執行此進程,直到失掉一個有序序列。
public class MergeSort {
public static void merge(int array[], int p, int q, int r) {
int i, j, k, n1, n2;
n1 = q - p + 1;
n2 = r - q;
int[] L = new int[n1];
int[] R = new int[n2];
for (i = 0, k = p; i < n1; i++, k++)
L[i] = array[k];
for (i = 0, k = q + 1; i < n2; i++, k++)
R[i] = array[k];
for (k = p, i = 0, j = 0; i < n1 && j < n2; k++) {
if (L[i] > R[j]) {
array[k] = L[i];
i++;
} else {
array[k] = R[j];
j++;
}
}
if (i < n1) {
for (j = i; j < n1; j++, k++)
array[k] = L[j];
}
if (j < n2) {
for (i = j; i < n2; i++, k++) {
array[k] = R[i];
}
}
}
public static void mergeSort(int array[], int p, int r) {
if (p < r) {
int q = (p + r) / 2;
mergeSort(array, p, q);
mergeSort(array, q + 1, r);
merge(array, p, q, r);
}
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
mergeSort(a, 0, a.length - 1);
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
}
}
五、疾速排序(QuickSort)
根本原理:關於一組給定的記載,經過一趟排序後,將原序列分為兩局部,其中前一局部的一切記載均比後一局部的一切記載小,然後再順次對前後兩局部的記載停止疾速排序,遞歸該進程,直到序列中的一切記載均有序為止。
public class QuickSort {
public static void sort(int array[], int low, int high) {
int i, j;
int index;
if (low >= high)
return;
i = low;
j = high;
index = array[i];
while (i < j) {
while (i < j && index <= array[j])
j--;
if (i < j)
array[i++] = array[j];
while (i < j && index > array[i])
i++;
if (i < j)
array[j--] = array[i];
}
array[i] = index;
sort(array, low, i - 1);
sort(array, i + 1, high);
}
public static void quickSort(int array[]) {
sort(array, 0, array.length - 1);
}
public static void main(String[] args) {
int a[] = { 5, 8, 4, 6, 7, 1, 3, 9, 2 };
quickSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
六、希爾排序(ShellSort)
根本原理:先將待排序的數組元素分紅多個子序列,使得每個子序列的元素個數絕對增加,然後對各個子序列辨別停止直接拔出排序,待整個待排序序列"根本有序後",最後再對一切元素停止一次直接拔出排序。
public class ShellSort {
public static void shellSort(int[] a) {
int len = a.length;
int i, j;
int h;
int temp;
for (h = len / 2; h > 0; h = h / 2) {
for (i = h; i < len; i++) {
temp = a[i];
for (j = i - h; j >= 0; j -= h) {
if (temp < a[j]) {
a[j + h] = a[j];
} else
break;
}
a[j + h] = temp;
}
}
}
public static void main(String[] args) {
int a[] = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
shellSort(a);
for (int j = 0; j < a.length; j++) {
System.out.print(a[j] + " ");
}
}
}
七、最小堆排序(MinHeapSort)
根本原理:關於給定的n個記載,初始時把這些記載看作一顆順序存儲的二叉樹,然後將其調整為一個小頂堆,然後將堆的最後一個元素與堆頂元素停止交流後,堆的最後一個元素即為最小記載;接著講前(n-1)個元素重新調整為一個小頂堆,再將堆頂元素與以後堆的最後一個元素停止交流後失掉次小的記載,反復該進程直到調整的堆中只剩一個元素時為止,該元素即為最大記載,此時可失掉一個有序序列。
public class MinHeapSort {
public static void adjustMinHeap(int[] a, int pos, int len) {
int temp;
int child;
for (temp = a[pos]; 2 * pos + 1 <= len; pos = child) {
child = 2 * pos + 1;
if (child < len && a[child] > a[child + 1])
child++;
if (a[child] < temp)
a[pos] = a[child];
else
break;
}
a[pos] = temp;
}
public static void myMinHeapSort(int[] array) {
int i;
int len = array.length;
for (i = len / 2 - 1; i >= 0; i--) {
adjustMinHeap(array, i, len - 1);
}
for (i = len - 1; i >= 0; i--) {
int tmp = array[0];
array[0] = array[i];
array[i] = tmp;
adjustMinHeap(array, 0, i - 1);
}
}
public static void main(String[] args) {
int[] a = { 5, 4, 9, 8, 7, 6, 0, 1, 3, 2 };
myMinHeapSort(a);
for (int i = 0; i < a.length; i++) {
System.out.print(a[i] + " ");
}
}
}
以上就是本文的全部內容,希望本文的內容對大家的學習或許任務能帶來一定的協助,同時也希望多多支持!