各大排序算法总结,你值得一看

本文详细介绍了冒泡排序、选择排序、插入排序、快速排序和归并排序的基本原理、Java实现、时间复杂度、空间复杂度以及各自的优缺点和适用场景,帮助读者理解和应用这些排序算法。
摘要由CSDN通过智能技术生成

花两kun半,也是总结了排序算法

排序算法概述

排序算法是计算机科学中用于对元素序列进行排序的一系列算法。它们在日常生活中非常常见,如对数据进行排序以便于分析和展示。以下是一些常见的排序算法的介绍,包括它们的原理、Java实现、时间复杂度、空间复杂度以及优缺点和适用场景。

冒泡排序 (Bubble Sort)

原理和基本思想

冒泡排序通过重复遍历要排序的数列,比较每对相邻元素,如果顺序错误就交换它们。这个过程重复进行,直到没有需要交换的元素为止。

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]) {
                    // swap arr[j] and arr[j+1]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
    }
}

时间复杂度和空间复杂度

  • 时间复杂度:平均和最坏情况都是O(n^2),最好情况是O(n)(如果数组已经排序)。
  • 空间复杂度:O(1)。

优缺点及适用场景

  • 优点:简单、直观,不需要额外的存储空间。
  • 缺点:效率低,尤其是对于大型数据集。
  • 适用场景:小规模数据或基本有序的数据集。

选择排序 (Selection Sort)

原理和基本思想

选择排序通过多次遍历数组,找出最小(或最大)的元素,然后将其与数组的当前位置进行交换。

Java实现代码

public class SelectionSort {
    public static void selectionSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            // Find the minimum element in unsorted array
            int min_idx = i;
            for (int j = i + 1; j < n; j++)
                if (arr[j] < arr[min_idx])
                    min_idx = j;

            // Swap the found minimum element with the first element
            int temp = arr[min_idx];
            arr[min_idx] = arr[i];
            arr[i] = temp;
        }
    }
}

时间复杂度和空间复杂度

  • 时间复杂度:O(n^2)。
  • 空间复杂度:O(1)。

优缺点及适用场景

  • 优点:简单,容易实现。
  • 缺点:效率低,不适合处理大量数据。
  • 适用场景:小型数据集,对性能要求不高。

插入排序 (Insertion Sort)

原理和基本思想

插入排序通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。

Java实现代码

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;

            /* Move elements of arr[0..i-1], that are greater than key,
               to one position ahead of their current position */
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j = j - 1;
            }
            arr[j + 1] = key;
        }
    }
}

时间复杂度和空间复杂度

  • 时间复杂度:平均和最坏情况都是O(n^2),最好情况是O(n)(如果数组已经排序)。
  • 空间复杂度:O(1)。

优缺点及适用场景

  • 优点:简单,对小数据集高效,稳定排序(保持相等元素的相对顺序)。
  • 缺点:不适合处理大量数据。
  • 适用场景:小型数据集,或数据集部分有序。

快速排序 (Quick Sort)

原理和基本思想

快速排序是一种分治算法,它通过选择一个“基准”值将数据分为两部分,一部分数据比基准值小,另一部分数据比基准值大,然后递归地对这两部分数据进行快速排序操作。

Java实现代码

public class QuickSort {
    private static void quickSort(int[] arr, int low, int high) {
        if (low < high) {
            /* pi is partitioning index, arr[pi] is now at right place */
            int pi = partition(arr, low, high);

            quickSort(arr, low, pi - 1);  // Before pi
            quickSort(arr, pi + 1, high); // After pi
        }
    }

    private static int partition(int[] arr, int low, int high) {
        int pivot = arr[high];
        int i = (low - 1);  // Index of smaller element
        for (int j = low; j < high; j++) {
            // If current element is smaller than or equal to pivot
            if (arr[j] <= pivot) {
                i++;

                // Swap arr[i] and arr[j]
                int temp = arr[i];
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

        // Swap arr[i+1] and arr[high] (or pivot)
        int temp = arr[i + 1];
        arr[i + 1] = arr[high];
        arr[high] = temp;

        return i + 1;
    }

    public static void quickSortMain(int[] arr) {
        quickSort(arr, 0, arr.length - 1);
    }
}

时间复杂度和空间复杂度

  • 时间复杂度:平均O(n log n),最坏O(n^2)。
  • 空间复杂度:O(log n)(递归性质导致)。

优缺点及适用场景

  • 优点:通常非常快,对于大型数据集效率很高。
  • 缺点:在最坏情况下效率会降低,不稳定排序。
  • 适用场景:大型数据集,需要快速排序的场景。

归并排序 (Merge Sort)

原理和基本思想

归并排序采用分治法的一个非常典型的应用,将已有序的子序列合并,得到有序序列。

Java实现代码

public class MergeSort {
    public static void mergeSort(int[] arr, int l, int r) {
        if (l < r) {
            // Find the middle point
            int m = (l + (r - 1)) / 2;

            // Sort first and second halves
            mergeSort(arr, l, m);
            mergeSort(arr, m + 1, r);

            // Merge the sorted halves
            merge(arr, l, m, r);
        }
    }

    public static void merge(int[] arr, int l, int m, int r) {
        // Find sizes of two subarrays to be merged
        int n1 = m - l + 1;
        int n2 = r - m;

        /* Create temp arrays */
        int[] L = new int[n1];
        int[] R = new int[n2];

        /*Copy data to temp arrays*/
        for (int i = 0; i < n1; ++i)
            L[i] = arr[l + i];
        for (int j = 0; j < n2; ++j)
            R[j] = arr[m + 1 + j];

        /* Merge the temp arrays */

        // Initial indexes of first and second subarrays
        int i = 0, j = 0;

        // Initial index of merged subarray array
        int k = l;
        while (i < n1 && j < n2) {
            if (L[i] <= R[j]) {
                arr[k] = L[i];
                i++;
            } else {
                arr[k] = R[j];
                j++;
            }
            k++;
        }

        /* Copy remaining elements of L[] if any */
        while (i < n1) {
            arr[k] = L[i];
            i++;
            k++;
        }

        /* Copy remaining elements of R[] if any */
        while (j < n2) {
            arr[k] = R[j];
            j++;
            k++;
        }
    }

    public static void mergeSortMain(int[] arr) {
        mergeSort(arr, 0, arr.length - 1);
    }
}

时间复杂度和空间复杂度

  • 时间复杂度:O(n log n)。
  • 空间复杂度:O(n)。

优缺点及适用场景

  • 优点:稳定排序,对大数据集合非常有效。
  • 缺点:需要额外的存储空间。
  • 适用场景:大数据集合,需要
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

取址执行

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值