App下載

Java中排序的四種方式!實例整理

被沖淡的悲傷 2023-10-11 09:51:02 瀏覽數(shù) (1412)
反饋

排序是編程中常見的任務,它可以幫助我們將數(shù)據(jù)按照一定的規(guī)則進行排列,以便更容易查找和處理。本文將為編程小白介紹Java中的四種排序方式,并通過具體示例演示它們的用法。

1. 冒泡排序(Bubble Sort)

排序算法--冒泡排序(Java語言) - 知乎

冒泡排序是一種簡單的排序算法,它反復遍歷待排序的數(shù)據(jù),比較相鄰的兩個元素,如果它們的順序不對就交換它們,直到整個數(shù)組有序為止。

示例代碼:

public class BubbleSort {
public static void main(String[] args) { int[] arr = {64, 34, 25, 12, 22, 11, 90}; int n = arr.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (arr[j] > arr[j + 1]) { int temp = arr[j]; arr[j] = arr[j + 1]; arr[j + 1] = temp; } } } System.out.println("冒泡排序后的數(shù)組:"); for (int num : arr) { System.out.print(num + " "); } } }

2. 選擇排序(Selection Sort)

java - 算法總結-直接選擇排序- 個人文章- SegmentFault 思否

選擇排序是另一種簡單的排序算法,它每次從未排序的部分中選擇最小的元素,然后與未排序部分的第一個元素交換。

示例代碼:

public class SelectionSort {
public static void main(String[] args) { int[] arr = {64, 34, 25, 12, 22, 11, 90}; int n = arr.length; for (int i = 0; i < n - 1; i++) { int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } int temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; } System.out.println("選擇排序后的數(shù)組:"); for (int num : arr) { System.out.print(num + " "); } } }

3. 插入排序(Insertion Sort)

java - 算法總結-直接插入排序- 個人文章- SegmentFault 思否

插入排序將未排序的元素逐個插入到已排序的部分,最終形成一個有序數(shù)組。

示例代碼:

public class InsertionSort {
public static void main(String[] args) { int[] arr = {64, 34, 25, 12, 22, 11, 90}; int n = arr.length; for (int i = 1; i < n; i++) { int key = arr[i]; int j = i - 1; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } arr[j + 1] = key; } System.out.println("插入排序后的數(shù)組:"); for (int num : arr) { System.out.print(num + " "); } } }

4. 快速排序(Quick Sort)

排序算法-快速排序(Java實現(xiàn)) - 知乎

快速排序是一種分治排序算法,它選擇一個基準元素,將小于基準的元素移到左邊,大于基準的元素移到右邊,然后遞歸地對左右兩部分排序。

示例代碼:

public class QuickSort {
public static void main(String[] args) { int[] arr = {64, 34, 25, 12, 22, 11, 90}; int n = arr.length; quickSort(arr, 0, n - 1); System.out.println("快速排序后的數(shù)組:"); for (int num : arr) { System.out.print(num + " "); } } public static void quickSort(int[] arr, int low, int high) { if (low < high) { int pi = partition(arr, low, high); quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } public static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = low - 1; for (int j = low; j < high; j++) { if (arr[j] < pivot) { i++; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } }

這四種排序算法是Java中常用的排序方法,它們在不同情況下有不同的性能表現(xiàn)。學會它們將有助于你更好地處理數(shù)據(jù)排序任務,為你的編程之路打下堅實的基礎。希望這些示例能夠幫助你理解和應用這些排序算法。

想了解更多編程知識?

如果你渴望深入了解編程和算法,以及更多的編程技能和資源,歡迎訪問編程獅官網(wǎng)。


無論你是剛剛入門編程的小白,還是已經(jīng)具備一定編程經(jīng)驗的開發(fā)者,編程獅官網(wǎng)都是你學習和進一步提升技能的理想去處。我們提供的資源包括:

  • 編程教程:針對不同編程語言和技術的詳細教程,逐步引導你學習和實踐編程知識。
  • 實用技巧和提示:學習編程的訣竅,了解如何提高代碼質(zhì)量,調(diào)試程序,以及應對各種編程難題。
  • 項目示例:參與實際項目,鍛煉編程技能。我們提供各種有趣的項目示例,從簡單的小應用到復雜的開源項目。
  • 社區(qū)支持:與其他編程愛好者互動,分享經(jīng)驗,提出問題,獲得答案,并建立有趣的編程社交圈。

無論你對Java中的排序算法感興趣,還是想探索更廣泛的編程世界,編程獅官網(wǎng)都將成為你編程學習的重要資源。不斷學習和提升,你將能夠應對更多的編程挑戰(zhàn),實現(xiàn)你的編程夢想。

祝愿你在編程的世界里取得成功,歡迎隨時訪問編程獅官網(wǎng),一起探索無限可能!


0 人點贊