掌握Java基本排序算法,轻松提升编程效率

引言
在编程中,排序算法是数据处理的基础,尤其是在处理大量数据时。Java作为一种广泛应用于企业级应用开发的语言,内置了多种排序算法。掌握这些基本排序算法,不仅能提高编程效率,还能加深对数据结构和算法的理解。本文将详细介绍Java中的几种基本排序算法,并提供相应的代码示例。
1. 冒泡排序(Bubble Sort)
冒泡排序是一种简单的排序算法,它重复地遍历要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。遍历数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。
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 - 1 - i; j++) { if (arr[j] > arr[j + 1]) {
// 交换 arr[j+1] 和 arr[j]
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(“Sorted array: “);
for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } 2. 选择排序(Selection Sort) 选择排序是一种简单直观的排序算法。它的工作原理是:第一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,然后再从剩余未排序元素中继续寻找最小(或最大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。 public class SelectionSort { public static void selectionSort(int[] arr) { int n = arr.length; for (int i = 0; i < n - 1; i++) { // 找到从 i 到 n-1 之间最小元素的索引 int minIndex = i; for (int j = i + 1; j < n; j++) { if (arr[j] < arr[minIndex]) { minIndex = j; } } // 将找到的最小元素与第 i 位置的元素交换 int temp = arr[minIndex]; arr[minIndex] = arr[i]; arr[i] = temp; } } public static void main(String[] args) { int[] arr = {64, 34, 25, 12, 22, 11, 90}; selectionSort(arr); System.out.println("Sorted array: "); for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } 3. 插入排序(Insertion Sort) 插入排序是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用in-place排序(即只需用到O(1)的额外空间的排序)。 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; // 将 arr[i] 插入到已排序序列 arr[0...i-1] 中 while (j >= 0 && arr[j] > key) {
arr[j + 1] = arr[j];
j = j – 1;
}
arr[j + 1] = key;
}
}

public static void main(String[] args) {
int[] arr = {64, 34, 25, 12, 22, 11, 90};
insertionSort(arr);
System.out.println(“Sorted array: “);
for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } } } 4. 快速排序(Quick Sort) 快速排序是一种分而治之的排序算法。它将原始数组分为较小和较大的两个子数组,然后递归地对这两个子数组进行快速排序。 public class QuickSort { public static void quickSort(int[] arr, int low, int high) { if (low < high) { // pi 是分区索引,arr[pi] 现在在正确的位置 int pi = partition(arr, low, high); // 分别对分区前后的子数组进行排序 quickSort(arr, low, pi - 1); quickSort(arr, pi + 1, high); } } private static int partition(int[] arr, int low, int high) { int pivot = arr[high]; int i = (low - 1); for (int j = low; j < high; j++) { // 如果当前元素小于或等于 pivot if (arr[j] <= pivot) { i++; // 交换 arr[i] 和 arr[j] int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } // 交换 arr[i+1] 和 arr[high](或 pivot) int temp = arr[i + 1]; arr[i + 1] = arr[high]; arr[high] = temp; return i + 1; } 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("Sorted array: "); for (int i = 0; i < n; i++) { System.out.print(arr[i] + " "); } } } 总结 通过以上对Java基本排序算法的介绍和代码示例,我们可以看到每种算法都有其特点和适用场景。在实际应用中,选择合适的排序算法可以显著提高程序的性能。希望本文能帮助您更好地理解和应用这些排序算法。