首页 话题 小组 问答 好文 用户 我的社区 域名交易 唠叨

[教程]Java实例数组排序:轻松掌握高效排序技巧,告别乱序烦恼

发布于 2025-06-19 21:18:55
0
7

引言在Java编程中,数组排序是一个基础而又重要的操作。无论是学习还是工作,掌握高效的排序技巧都能大大提高代码的效率和可读性。本文将详细介绍Java中几种常见的数组排序方法,并通过实例代码进行演示,帮...

引言

在Java编程中,数组排序是一个基础而又重要的操作。无论是学习还是工作,掌握高效的排序技巧都能大大提高代码的效率和可读性。本文将详细介绍Java中几种常见的数组排序方法,并通过实例代码进行演示,帮助读者轻松掌握这些技巧。

1. 冒泡排序

冒泡排序是一种简单的排序算法,它重复地遍历要排序的数列,一次比较两个元素,如果它们的顺序错误就把它们交换过来。遍历数列的工作是重复地进行,直到没有再需要交换的元素为止。

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] 和 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("Sorted array: "); for (int i : arr) { System.out.print(i + " "); } }
}

2. 选择排序

选择排序是一种简单直观的排序算法。它的工作原理是:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。以此类推,直到所有元素均排序完毕。

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; } } 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 : arr) { System.out.print(i + " "); } }
}

3. 插入排序

插入排序是一种简单直观的排序算法。它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。插入排序在实现上,通常采用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; while (j >= 0 && arr[j] > key) { arr[j + 1] = arr[j]; j--; } 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 : arr) { System.out.print(i + " "); } }
}

4. 快速排序

快速排序是由东尼·霍尔所提出的一种排序算法。它是一种分而治之的算法方法。选择一个元素作为基准(pivot),重新排序数组,所有比基准值小的元素摆放在基准前面,所有比基准值大的元素摆放在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于数列的中间位置。这个称为分区(partition)操作。

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); } } 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++) { 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 = {64, 34, 25, 12, 22, 11, 90}; int n = arr.length; quickSort(arr, 0, n - 1); System.out.println("Sorted array: "); for (int i : arr) { System.out.print(i + " "); } }
}

总结

本文介绍了Java中几种常见的数组排序方法,包括冒泡排序、选择排序、插入排序和快速排序。每种排序方法都有其特点和适用场景。通过实例代码的演示,读者可以轻松掌握这些排序技巧,提高自己的编程能力。在实际应用中,根据具体需求和数据特点选择合适的排序方法,能够使程序更加高效和健壮。

评论
一个月内的热帖推荐
csdn大佬
Lv.1普通用户

452398

帖子

22

小组

841

积分

赞助商广告
站长交流