熟女俱乐部五十路二区av,又爽又黄禁片视频1000免费,国产卡一卡二卡三无线乱码新区,中文无码一区二区不卡αv,中文在线中文a

"); //-->

博客專欄

EEPW首頁 > 博客 > Java視頻教程之常用Java排序算法分享

Java視頻教程之常用Java排序算法分享

發(fā)布人:扣丁學堂1 時間:2021-01-22 來源:工程師 發(fā)布文章

在本篇文章開始之前先問大家一個問題,小伙伴們對Java排序算法有多少的了解呢?常用的Java排序算法有幾種又有多少小伙伴知道呢?本篇文章扣丁學堂Java在線學習小編就給大家分享一下常用的Java排序算法,希望對Java感興趣的小伙伴仔細閱讀。

扣丁學堂簡述常用Java排序算法有哪些

本文主要介紹Java的七種常見排序算法的實現(xiàn),對選擇排序、插入排序、冒泡排序、歸并排序、快速排序、希爾排序、最小堆排序進行原理分析與實例介紹,下面一起來看一下吧:


一、選擇排序(SelectSort)


基本原理:對于給定的一組記錄,經(jīng)過第一輪比較后得到最小的記錄,然后將該記錄與第一個記錄的位置進行交換;接著對不包括第一個記錄以外的其他記錄進行第二次比較,得到最小的記錄并與第二個記錄進行位置交換;重復該過程,直到進行比較的記錄只有一個為止。

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)


基本原理:對于給定的一組數(shù)據(jù),初始時假設第一個記錄自成一個有序序列,其余記錄為無序序列。接著從第二個記錄開始,按照記錄的大小依次將當前處理的記錄插入到其之前的有序序列中,直至最后一個記錄插入到有序序列中為止。

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)


基本原理:利用遞歸與分治技術(shù)將數(shù)據(jù)序列劃分成為越來越小的半子表,再對半子表排序,最后再用遞歸方法將排好序的半子表合并成為越來越大的有序序列。對于給定的一組記錄(假設共有n個記錄),首先將每兩個相鄰的長度為1的子序列進行歸并,得到n/2(向上取整)個長度為2或1的有序子序列,再將其兩兩歸并,反復執(zhí)行此過程,直到得到一個有序序列。

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)


基本原理:先將待排序的數(shù)組元素分成多個子序列,使得每個子序列的元素個數(shù)相對減少,然后對各個子序列分別進行直接插入排序,待整個待排序序列"基本有序后",最后再對所有元素進行一次直接插入排序。

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個記錄,初始時把這些記錄看作一顆順序存儲的二叉樹,然后將其調(diào)整為一個小頂堆,然后將堆的最后一個元素與堆頂元素進行交換后,堆的最后一個元素即為最小記錄;接著講前(n-1)個元素重新調(diào)整為一個小頂堆,再將堆頂元素與當前堆的最后一個元素進行交換后得到次小的記錄,重復該過程直到調(diào)整的堆中只剩一個元素時為止,該元素即為最大記錄,此時可得到一個有序序列。

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] + " ");
 }
 }
}


以上就是小編給大家分享的常用的Java排序算法詳解,想要了解更多內(nèi)容的小伙伴可以登錄扣丁學堂官網(wǎng)查看更多內(nèi)容??鄱W堂是專業(yè)的Java培訓機構(gòu),不僅有專業(yè)的老師和與時俱進的課程體系,還有大量的Java在線視頻供學員觀看學習哦??鄱W堂java技術(shù)交流群:487098661。微信號:codingbb

*博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點,如有侵權(quán)請聯(lián)系工作人員刪除。



關鍵詞:

相關推薦

技術(shù)專區(qū)

關閉