深入探究常用排序算法:冒泡、插入、选择与快速排序

简介: 深入探究常用排序算法:冒泡、插入、选择与快速排序

冒泡排序(Bubble Sort)

冒泡排序是一种简单的排序算法,其基本思想是不断地交换相邻的元素,使较大的元素慢慢“冒泡”到数组的末尾。

  • 实现步骤:
    从数组的第一个元素开始,逐个比较相邻元素,若前者大于后者则交换位置。
    重复进行上述操作,直到没有交换发生,说明数组已经有序。
  • 使用场景
    冒泡排序是一种简单的排序算法,适用于小规模的数据集。 当数据集规模较小时,冒泡排序的性能损耗相对较小,且实现简单
  • 时间复杂度
    最坏时间复杂度:O(n^2)
    最好时间复杂度:O(n)(当数据已经有序时)
    平均时间复杂度:O(n^2)
  • 实现代码
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.print("冒泡排序结果: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

插入排序(Insertion Sort)

插入排序是一种简单而有效的排序算法,它通过构建有序序列,将未排序的元素逐个插入到合适的位置。

  • 实现步骤:
    将第一个元素视为已排序序列,从第二个元素开始,逐个与已排序序列比较,找到合适的插入位置。
    插入元素到合适位置后,已排序序列右移,为新元素腾出位置。
    重复上述操作,直到所有元素有序。
  • 使用场景
    插入排序对于“几乎有序”的数据集效果很好,因为它只需要少量的操作即可将元素插入到正确位置。
    对于小规模数据集,插入排序是一个较好的选择。
  • 时间复杂度
    最坏时间复杂度:O(n^2)
    最好时间复杂度:O(n)(当数据已经有序时)
    平均时间复杂度:O(n^2)
  • 实现代码
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;
            // 逐个将比 key 大的元素向后移动,为 key 腾出插入位置
            while (j >= 0 && key < arr[j]) {
                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.print("插入排序结果: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

选择排序(Selection Sort)

选择排序是一种简单直观的排序算法,每次从未排序部分选择最小(或最大)的元素,放到已排序部分的末尾。

  • 实现步骤:
    找到未排序部分的最小元素,与未排序部分的第一个元素交换位置。
    已排序部分逐渐增长,重复执行步骤1,直到所有元素有序。
  • 使用场景
    选择排序适用于小规模数据集,因为它的性能不受数据是否部分有序的影响。
    虽然选择排序的性能不如其他高级排序算法,但在某些情况下仍然是一个可行的选择。
  • 时间复杂度
    最坏时间复杂度:O(n^2)
    最好时间复杂度:O(n^2)
    平均时间复杂度:O(n^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.print("选择排序结果: ");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}

快速排序(Quick Sort)

快速排序是一种高效的分治排序算法,通过将数组分成较小和较大的两部分,分别对它们进行排序。

  • 实现步骤:
    选择一个基准元素,将数组分为比基准小和比基准大的两部分。
    递归地对两个子数组进行快速排序。
    合并已排序的子数组和基准元素,得到最终有序数组。
  • 使用场景
    快速排序适用于大规模数据集,尤其是在平均情况下它具有很高的性能。
    在大多数实际应用中,快速排序是首选的排序算法,因为它通常比其他排序算法更快。
  • 时间复杂度
    最坏时间复杂度:O(n^2)
    最好时间复杂度:O(n log n)
    平均时间复杂度:O(n log n)
  • 实现代码
public class QuickSort {
    public static int[] quickSort(int[] arr) {
        if (arr.length <= 1) {
            return arr;
        }
        int pivot = arr[arr.length / 2];
        List<Integer> left = new ArrayList<>();
        List<Integer> middle = new ArrayList<>();
        List<Integer> right = new ArrayList<>();
        for (int num : arr) {
            if (num < pivot) {
                left.add(num);
            } else if (num == pivot) {
                middle.add(num);
            } else {
                right.add(num);
            }
        }
        int[] sortedLeft = quickSort(left.stream().mapToInt(Integer::intValue).toArray());
        int[] sortedRight = quickSort(right.stream().mapToInt(Integer::intValue).toArray());
        int[] result = new int[arr.length];
        System.arraycopy(sortedLeft, 0, result, 0, sortedLeft.length);
        for (int i = 0; i < middle.size(); i++) {
            result[sortedLeft.length + i] = middle.get(i);
        }
        System.arraycopy(sortedRight, 0, result, sortedLeft.length + middle.size(), sortedRight.length);
        return result;
    }
    public static void main(String[] args) {
        int[] arr = {64, 34, 25, 12, 22, 11, 90};
        int[] sortedArr = quickSort(arr);
        System.out.print("快速排序结果: ");
        for (int num : sortedArr) {
            System.out.print(num + " ");
        }
    }
}

上述代码的排序结果均为:

排序结果: 11 12 22 25 34 64 90

在选择排序算法时,需要根据数据集的规模、数据的有序性以及对排序稳定性的要求来进行合适的选择。在实际开发中,可能会根据具体情况选择不同的排序算法来满足需求。

后记 👉👉💕💕美好的一天,到此结束,下次继续努力!欲知后续,请看下回分解,写作不易,感谢大家的支持!! 🌹🌹🌹

相关文章
|
3天前
|
搜索推荐 算法 Java
Java中的快速排序、归并排序和堆排序是常见的排序算法。
【6月更文挑战第21天】Java中的快速排序、归并排序和堆排序是常见的排序算法。快速排序采用分治,以基准元素划分数组并递归排序;归并排序同样分治,先分割再合并有序子数组;堆排序通过构建堆来排序,保持堆性质并交换堆顶元素。每种算法各有优劣:快排平均高效,最坏O(n²);归并稳定O(n log n)但需额外空间;堆排序O(n log n)且原地排序,但不稳定。
14 3
|
7天前
|
算法 搜索推荐 JavaScript
算法学习:快速排序
算法学习:快速排序
11 1
|
7天前
|
搜索推荐 算法
【排序】数据结构——排序算法概念及代码详解(插入、冒泡、快速、希尔)
【排序】数据结构——排序算法概念及代码详解(插入、冒泡、快速、希尔)
|
13天前
|
算法
数据结构与算法-快速排序
数据结构与算法-快速排序
8 1
|
24天前
|
存储 搜索推荐 算法
快速排序算法详解
快速排序算法详解
|
3天前
|
人工智能 算法 搜索推荐
蓝桥杯宝藏排序题目算法(冒泡、选择、插入)
以下是内容的摘要: 本文介绍了三种排序算法:冒泡排序、选择排序和插入排序。冒泡排序通过不断交换相邻的逆序元素逐步排序,最坏情况下需要 O(n^2) 次比较。选择排序在每轮中找到剩余部分的最小元素并放到已排序序列的末尾,同样具有 O(n^2) 时间复杂度。插入排序则是将每个元素插入到已排序序列的正确位置,时间复杂度也是 O(n^2),但空间复杂度为 O(1)。
|
7天前
|
搜索推荐
排序算法----快速排序----详解&&代码
排序算法----快速排序----详解&&代码
|
14天前
|
算法 搜索推荐 数据可视化
【漫画算法】指挥官的排序战术:快速排序算法解密
【漫画算法】指挥官的排序战术:快速排序算法解密
|
18天前
|
算法 搜索推荐
数据结构和算法——快速排序(算法概述、选主元、子集划分、小规模数据的处理、算法实现)
数据结构和算法——快速排序(算法概述、选主元、子集划分、小规模数据的处理、算法实现)
10 0
|
1月前
|
算法 C++
c++算法学习笔记 (1)快速排序
c++算法学习笔记 (1)快速排序