排序算法是計算機科學中最基本、最重要的算法之一。排序算法的目的是將一組數據按照特定的順序進行排列,以便于后續的查找、統計和分析。Java作為一種廣泛使用的編程語言,提供了多種排序算法的實現方式。本文將詳細介紹Java中常見的排序算法及其實現方法。
排序算法可以分為兩大類:比較排序和非比較排序。
排序算法的性能通常通過時間復雜度和空間復雜度來衡量。時間復雜度表示算法執行所需的時間,空間復雜度表示算法執行所需的內存空間。
冒泡排序是一種簡單的排序算法。它重復地遍歷要排序的列表,比較相鄰的元素并交換它們的位置,直到列表有序。
public class BubbleSort {
public static void bubbleSort(int[] arr) {
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]) {
// 交換arr[j]和arr[j+1]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(arr);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
選擇排序是一種簡單直觀的排序算法。它的工作原理是每次從未排序的部分中選擇最?。ɑ蜃畲螅┑脑?,放到已排序部分的末尾。
public class SelectionSort {
public static void selectionSort(int[] arr) {
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;
}
}
// 交換arr[i]和arr[minIndex]
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
public static void main(String[] args) {
int[] arr = {64, 25, 12, 22, 11};
selectionSort(arr);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
插入排序是一種簡單直觀的排序算法。它的工作原理是通過構建有序序列,對于未排序數據,在已排序序列中從后向前掃描,找到相應位置并插入。
public class InsertionSort {
public static void insertionSort(int[] arr) {
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;
}
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6};
insertionSort(arr);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
歸并排序是一種分治算法。它將列表分成兩個子列表,分別對子列表進行排序,然后將排序后的子列表合并成一個有序列表。
public class MergeSort {
public static void mergeSort(int[] arr, int left, int right) {
if (left < right) {
int mid = (left + right) / 2;
mergeSort(arr, left, mid);
mergeSort(arr, mid + 1, right);
merge(arr, left, mid, right);
}
}
public static void merge(int[] arr, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int[] L = new int[n1];
int[] R = new int[n2];
for (int i = 0; i < n1; i++) {
L[i] = arr[left + i];
}
for (int j = 0; j < n2; j++) {
R[j] = arr[mid + 1 + j];
}
int i = 0, j = 0;
int k = left;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
mergeSort(arr, 0, arr.length - 1);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
快速排序是一種分治算法。它通過選擇一個“基準”元素,將列表分成兩個子列表,分別對子列表進行排序。
public class QuickSort {
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;
}
public static void main(String[] args) {
int[] arr = {10, 7, 8, 9, 1, 5};
quickSort(arr, 0, arr.length - 1);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
堆排序是一種基于二叉堆數據結構的排序算法。它通過構建一個最大堆(或最小堆),然后將堆頂元素與最后一個元素交換,再調整堆,重復這個過程直到列表有序。
public class HeapSort {
public static void heapSort(int[] arr) {
int n = arr.length;
// 構建最大堆
for (int i = n / 2 - 1; i >= 0; i--) {
heapify(arr, n, i);
}
// 逐個提取元素
for (int i = n - 1; i > 0; i--) {
// 交換堆頂元素和最后一個元素
int temp = arr[0];
arr[0] = arr[i];
arr[i] = temp;
// 調整堆
heapify(arr, i, 0);
}
}
public static void heapify(int[] arr, int n, int i) {
int largest = i; // 初始化最大元素為根節點
int left = 2 * i + 1; // 左子節點
int right = 2 * i + 2; // 右子節點
// 如果左子節點大于根節點
if (left < n && arr[left] > arr[largest]) {
largest = left;
}
// 如果右子節點大于當前最大元素
if (right < n && arr[right] > arr[largest]) {
largest = right;
}
// 如果最大元素不是根節點
if (largest != i) {
int swap = arr[i];
arr[i] = arr[largest];
arr[largest] = swap;
// 遞歸調整受影響的子樹
heapify(arr, n, largest);
}
}
public static void main(String[] args) {
int[] arr = {12, 11, 13, 5, 6, 7};
heapSort(arr);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
計數排序是一種非比較排序算法。它通過統計每個元素的出現次數,然后根據統計結果將元素放回原列表。
public class CountingSort {
public static void countingSort(int[] arr) {
int n = arr.length;
int max = Arrays.stream(arr).max().getAsInt();
int[] count = new int[max + 1];
// 統計每個元素的出現次數
for (int i = 0; i < n; i++) {
count[arr[i]]++;
}
// 將統計結果放回原列表
int index = 0;
for (int i = 0; i <= max; i++) {
while (count[i] > 0) {
arr[index++] = i;
count[i]--;
}
}
}
public static void main(String[] args) {
int[] arr = {4, 2, 2, 8, 3, 3, 1};
countingSort(arr);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
基數排序是一種非比較排序算法。它通過將整數按位數切割成不同的數字,然后按每個位數分別進行排序。
public class RadixSort {
public static void radixSort(int[] arr) {
int max = Arrays.stream(arr).max().getAsInt();
for (int exp = 1; max / exp > 0; exp *= 10) {
countingSortByDigit(arr, exp);
}
}
public static void countingSortByDigit(int[] arr, int exp) {
int n = arr.length;
int[] output = new int[n];
int[] count = new int[10];
// 統計每個數字的出現次數
for (int i = 0; i < n; i++) {
count[(arr[i] / exp) % 10]++;
}
// 計算每個數字的最終位置
for (int i = 1; i < 10; i++) {
count[i] += count[i - 1];
}
// 將元素放回原列表
for (int i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// 將排序結果復制回原數組
System.arraycopy(output, 0, arr, 0, n);
}
public static void main(String[] args) {
int[] arr = {170, 45, 75, 90, 802, 24, 2, 66};
radixSort(arr);
System.out.println("排序后的數組:");
for (int i : arr) {
System.out.print(i + " ");
}
}
}
桶排序是一種非比較排序算法。它將列表分成若干個桶,每個桶內的元素進行排序,然后將所有桶的元素合并成一個有序列表。
”`java import java.util.ArrayList; import java.util.Collections;
public class BucketSort { public static void bucketSort(int[] arr) { int n = arr.length; int max = Arrays.stream(arr).max().getAsInt(); int min = Arrays.stream(arr).min().getAsInt(); int bucketSize = (max - min) / n + 1;
ArrayList<ArrayList<Integer>> buckets = new ArrayList<>(n);
for (int i = 0; i < n; i++) {
buckets.add(new ArrayList<>());
}
// 將元素分配到各個桶中
for (int i = 0; i < n; i++) {
int bucketIndex = (arr[i] - min) / bucketSize;
buckets.get(bucketIndex).add(arr[i]);
}
// 對每個桶內的元素進行排序
for (ArrayList<Integer> bucket : buckets) {
Collections.sort(bucket);
}
// 將所有桶的元素合并成一個有序列表
int index = 0;
for (ArrayList<Integer> bucket : buckets) {
for (int num : bucket) {
arr[index++] = num;
}
}
}
public static
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。