博客
关于我
数据结构:常见排序算法(2) -- 选择排序(选择排序、堆排序)
阅读量:556 次
发布时间:2019-03-09

本文共 2816 字,大约阅读时间需要 9 分钟。

选择排序和堆排序是两种经典的排序算法,在不同的场景下各有优劣。以下从原理、代码实现及性能分析等方面详细阐述。

选择排序

原理

选择排序是一种简单有效的排序算法。其工作原理是:每次从当前数组中找出最小或最大元素,将其移动到已排序区间的最末端或最前端。这个过程重复进行,直至整个数组排序完成。

具体而言:

  • 将数组分为已排序区间和无序区间。
  • 在每次循环中,选择当前无序区间中的最大或最小值。
  • 将选择的值移动到已排序区间的末尾或开头。
  • 重复上述步骤,直到无序区间为空。
  • 这种方法的最优性在于其逻辑简单,时间复杂度为O(n²),适合针对数据规模较小时使用。

    代码实现

    以下是基于Java的选择排序代码示例:

    import java.util.Arrays;public class SelectSort {    public static void main(String[] args) {        int[] array = {5, 1, 25, 4, 8, 11, 5, 7, 5, 0};        System.out.println(Arrays.toString(array));        selectSort(array);        System.out.println(Arrays.toString(array));    }    public static void selectSort(int[] array) {        int left = 0, right = array.length - 1;        while (left < right) {            int maxPos = left;            for (int i = left + 1; i <= right; i++) {                if (array[i] > array[maxPos]) {                    maxPos = i;                }            }            int max = array[maxPos];            array[maxPos] = array[left];            array[left] = max;            left++;        }    }}

    性能分析

    • 时间复杂度:O(n²),排序时间与数组长度的平方成正比。
    • 空间复杂度:O(1),算法仅使用常数额外空间。
    • 稳定性:不稳定,较大值多次被交换可能破坏相对顺序。

    堆排序

    原理

    堆排序与选择排序类似,但采用了不同的策略:通过构建堆(最大堆或最小堆)来快速找到无序区间的最大或最小值。关键步骤包括:

  • 将数组构建为大根堆或小根堆。
  • 从堆顶取出最大的元素,并放在已排序区间。
  • 调整堆结构,重新插入元素并恢复堆属性。
  • 重复上述步骤,直到无序区间为空。
  • 这与选择排序相比,减少了逐次查找最大值的时间复杂度,从O(n) 降低至 O(log n),但增加了堆操作的复杂性。

    代码实现

    以下是基于Java的堆排序代码示例:

    import java.util.Arrays;public class HeapSort {    public static void main(String[] args) {        int[] array = {27, 15, 19, 18, 28, 34, 65, 49, 25, 37};        printArray(array);        heapSort(array);        printArray(array);    }    private void printArray(int[] array) {        System.out.println(Arrays.toString(array));    }    public void heapSort(int[] array) {        int size = array.length;        int[] heap = new int[size];        for (int i = 0; i < size; i++) {            heap[i] = array[i];        }        for (int i = size - 1; i > 0; i--) {            adjustHeap(i, size);        }        int end = size - 1;        while (end > 0) {            int temp = heap[0];            heap[0] = heap[end];            heap[end] = temp;            adjustHeap(0, end);            end--;        }    }    private void adjustHeap(int parent, int len) {        int child = 2 * parent + 1;        while (child < len) {            if (child < len - 1 && heap[child] > heap[child + 1]) {                child++;            }            if (heap[child] > heap[parent]) {                swapElements(child, parent);                parent = child;                child = 2 * parent + 1;            } else {                break;            }        }    }    private void swapElements(int i, int j) {        int temp = heap[i];        heap[i] = heap[j];        heap[j] = temp;    }}

    性能分析

    • 时间复杂度:O(n log n),排序时间与数组长度的对数成正比。
    • 空间复杂度:O(n),需要额外存储用于构建堆。
    • 稳定性:不稳定,较大值多次被交换可能破坏相对顺序。

    转载地址:http://bwkpz.baihongyu.com/

    你可能感兴趣的文章
    python语言有哪些优点和缺点_Python有哪些优缺点,你了解吗?
    查看>>
    Python 从入门到精通:30天速成教程到底有多狠?你能坚持下来吗?
    查看>>
    Python 从数据库中存储和检索密码的最安全方法
    查看>>
    Python语言及其应用 - 知识点遍历
    查看>>
    Python 优化提速的 8 个小技巧
    查看>>
    Python 余弦相似度与皮尔逊相关系数 计算
    查看>>
    python 使用execjs 报编码错误解决办法,UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xac in position 145: il
    查看>>
    python 使用filetype校验文件
    查看>>
    Python 使用flush函数将缓冲区数据立即写磁盘
    查看>>
    python 使用in判断不准确,in不好使
    查看>>
    Python 使用pandas 进行查询和统计详解
    查看>>
    Redis 配置文件redis.conf详细解释
    查看>>
    python网络爬虫(2)——scrapy框架的基础使用
    查看>>
    python网络爬虫实例教程试读_Python网络爬虫实战教程(全套完整版) - 学途无忧网 - 做技术的王者 - Powered By EduSoho...
    查看>>
    Python 使用哈希函数用于加密
    查看>>
    Python 依赖管理的革新——Poetry 深度解析
    查看>>
    python 保留精度及增加去除数字的千位分隔符(金额化数字)
    查看>>
    python 倒计时 9,8,7,。。。。。。0
    查看>>
    Python 入门开发学习笔记之数据的增删改查
    查看>>
    Python 入门教程(2)搭建环境 2.4、VSCode配置Node.js运行环境
    查看>>