算法3-选择、插入与希尔


各位读者早上好!这里粘贴下前段时间温习的初级排序算法:
选择、插入与希尔,算法虽说是基础中的基础,不过依旧无法遮挡当年发明者思想的光芒。示例代码均来自:

本博客代码示例均来自:算法 Algorithmes Forth Edition
[美] Robert Sedgewick Kevin Wayne 著 谢路云译

书中介绍算法的思路很好,而且验证起来很方便。尤其是把排序分为了比较和交换2个过程。在初级排序里,个人觉得希尔不好理解一些,所以debug观察了下运行过程,不过对于内循环的终止和继续条件还是有些模糊。笔者验证代码均在github有提交:

https://github.com/xiaomeiatgit/my2020.git

一、选择排序


package com.chm.algorithms;

/**
 * Author:meice Huang
 * Time: 2020/2/26 上午5:34
 */


import static com.chm.algorithms.Example.exch;
import static com.chm.algorithms.Example.less;
import static com.chm.algorithms.Example.show;

public class Selection {
    /**
     * 选择排序
     * 前提:整数,默认ASC排序且数组.length>=1
     */
    public static void sort(Comparable[] a) {
        int L = a.length;
        for (int i = 0; i < L; i++) {
            for (int j = i + 1; j < L; j++) {
                int minIndex = i;
                if (less(a[j], a[i])) {
                    minIndex = j;
                }
                exch(a, i, minIndex);
            }
        }
        show(a);
    }

    /**
     * 总结:
     * 这种简化过程,抽离方法的做法很好。
     * 其实排序的核心思想就是:比较+交换
     *
     * 算法效率:对于长度为N的数组,需要大约N^2/2次比较和N次交换
     */
    public void testAscSelectionSorted() {
        sort(new Comparable[]{12, 345, 98, 3, 987, 1234, -9, 23});
    }
}

二、插入排序


package com.chm.algorithms;

import static com.chm.algorithms.Example.exch;
import static com.chm.algorithms.Example.less;
import static com.chm.algorithms.Example.show;

/**
 * Author:meice Huang
 * Time: 2020/2/26 上午7:33
 */


public class Insertion {
    /**
     * 插入排序
     */
    public static void sort(Comparable[] a) {
        int N = a.length;
        for (int i = 1; i < N; i++) {
            for (int j = i; j > 0 && less(a[j], a[j - 1]); j--) {
                exch(a, j, j - 1);
            }
        }
        show(a);
    }

    public void testInsertionSort() {
        sort(new Comparable[]{12, 345, 98, 3, 987, 1234, -9, 23});
    }
}

三、希尔排序

package com.chm.algorithms;

/**
 * Author:meice Huang
 * Time: 2020/3/4 上午7:40
 */

import java.util.Arrays;

import static com.chm.algorithms.Example.exch;
import static com.chm.algorithms.Example.less;

public class Shell {
    public static void sort(Comparable[] a) {
        int N = a.length;
        int h = 1;
        while (h < N / 3)
            h = 3 * h + 1;
        while (h >= 1) {
            for (int i = h; i < N; i++) {
                for (int j = i; j >= h && less(a[j], a[j - h]); j -= h) {
                    exch(a, j, j - h);
                }
            }
            h = h / 3;
        }
    }

    public static void main(String[] args) {
        Integer[] numbers = {49, 38, 65, 97, 76, 13, 27, 48, 55, 4};
        sort(numbers);
    }
}

理解运行过程,参考了下百度百科的示意图,自己也画了下,加深理解。

在这里插入图片描述

好,下一次介绍:归并、快速排序、优先队列;之后我们介绍查找;再后面介绍图、最后介绍字符串。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值