1. 冒泡排序(Bubble Sort)
冒泡排序是一种简单直观的排序算法,在待排序序列中不断地交换相邻两个元素的位置,通过多次遍历,将最大(或最小)的元素逐渐向右(或左)移动到正确的位置,直到整个序列有序。
冒泡排序的基本思想如下:
给定数组 A=[64,34,25,12,22,11,90],冒泡排序的过程如下:
第一轮:
第二轮:
依此类推,直到数组完全排序。
冒泡排序的时间复杂度为O(n^2),其中n表示待排序序列的长度。冒泡排序是稳定的,因为相同元素的相对顺序不会发生改变。
2. 插入排序(Insertion Sort)
插入排序是一种简单且直观的排序算法,将待排序序列分为已排序和未排序两部分,每次从未排序的部分中取出一个元素插入到已排序的正确位置,直到整个序列有序。
插入排序的基本思想如下:
给定数组 A=[64,34,25,12,22,11,90],插入排序的过程如下:
插入排序的时间复杂度为O(n^2),其中n表示待排序序列的长度。插入排序是稳定的,因为相同元素的相对顺序不会发生改变。
3. 选择排序(Selection Sort)
选择排序是一种简单直观的排序算法,每次从待排序序列中选择最小(或最大)的元素,放到已排序序列的末尾,直到整个序列有序。
选择排序的基本思想如下:
给定数组 A=[64,34,25,12,22,11,90],选择排序的过程如下:
选择排序的时间复杂度为O(n^2),其中n表示待排序序列的长度。选择排序是不稳定的,因为相同元素的相对顺序可能会发生改变。
4. 使用 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]) {
- 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 num : arr) {
- System.out.print(num + " ");
- }
- }
- }
- 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("插入排序后的数组:");
- // 输出排序后的数组
- for (int num : arr) {
- System.out.print(num + " ");
- }
- }
- }
- 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("选择排序后的数组:");
- // 输出排序后的数组
- for (int num : arr) {
- System.out.print(num + " ");
- }
- }
- }
在选择三种排序算法时,一般的算法优先选择顺序是: