排序算法-简单排序

插入排序

算法解释

插入排序(InsertionSort),一般也被称为直接插入排序。

插入排序的工作方式非常像人们排序一手扑克牌一样。每次拾起新牌后,人们习惯按照升降序顺序将新牌插入到指定的位置。实际上,每次拿到新牌,大脑会将新牌和手中的已经排好顺序的牌进行比较。只不过大脑运行也很快以至于好像没有进行运算的感觉。

插入排序

核心思想

  1. 把所有的元素分为两组,已经排序的和未排序的;
  2. 找到未排序的组中的第一个元素,向已经排序的组中进行插入;
  3. 倒叙遍历已经排序的元素,依次和待插入的元素进行比较,直到找到一个元素小于等于待插入元素,那么就把待
    插入元素放到这个位置,其他的元素向后移动一位。

算法模拟

  1. 箭头指向的元素为待排序元素;
  2. 蓝色选中区域内的元素为已经排好序的元素组;
  3. 箭头指向的元素需要和蓝色选中区域内的元素从右到左逐个进行比较,如果蓝色区域的元素比待排序元素大,则交换位置,直到在蓝色选中区域内找到比待排序元素小为止。

在这里插入图片描述

程序实现

package com.skystep.algorithm;

public class Insertion {

    public static void sortAsc(Comparable[] comparables) {
        int len = comparables.length;

        for (int i = 1; i < len; i++) {
            for (int j = i; j > 0; j--) {
                if (compare(comparables[j], comparables[j-1])) {
                    break;
                }
                change(comparables, j, j-1);
            }
        }
    }

    public static void sortDesc(Comparable[] comparables) {
        int len = comparables.length;
        for (int i = 1; i < len; i++) {
            for (int j = i; j > 0; j--) {
                if (!compare(comparables[j], comparables[j-1])) {
                    break;
                }
                change(comparables, j, j-1);
            }
        }
    }


    public static void change(Comparable[] comparables, int x, int y) {
        Comparable tmp = comparables[x];
        comparables[x] = comparables[y];
        comparables[y] = tmp;
    }

    public static boolean compare(Comparable source, Comparable target) {
        return source.compareTo(target) >= 0;
    }

    public static void main(String[] args) {
        Integer[] arr = {1, 2, 4, 3, 6, 5};
        Insertion.sortAsc(arr);
        for (Integer integer : arr) {
            System.out.print(integer);
            System.out.print(" ");
        }
        System.out.println();

        Insertion.sortDesc(arr);
        for (Integer integer : arr) {
            System.out.print(integer);
            System.out.print(" ");
        }
        System.out.println();

        Student s1 = new Student(10, "s1");
        Student s2 = new Student(13, "s2");
        Student s3 = new Student(12, "s3");
        Student s4 = new Student(17, "s4");
        Student s5 = new Student(14, "s5");
        Student[] students = {s1, s2, s3, s4, s5};

        Insertion.sortDesc(students);
        for (Student student : students) {
            System.out.println("age:" + student.getAge() + "," + "name:" + student.getName());
        }
        System.out.println();

        Insertion.sortAsc(students);
        for (Student student : students) {
            System.out.println("age:" + student.getAge() + "," + "name:" + student.getName());
        }
        System.out.println();
    }
}

适用说明

插入排序的平均时间复杂度是 O(n^2),空间复杂度为常数阶 O(1),具体时间复杂度和数组的有序性也是有关联的。对于少量元素的排序,它是一个有效的算法。

插入排序中,当待排序数组是有序时,是最优的情况,只需当前数跟前一个数比较一下就可以了,这时一共需要比较 N-1 次,时间复杂度为 O(N)。最坏的情况是待排序数组是逆序的,此时需要比较次数最多,最坏的情况是 O(n^2)。

冒泡排序

算法解释

冒泡排序,类似于水中冒泡,较大的数沉下去,较小的数慢慢冒起来,假设从小到大,即为较大的数慢慢往后排,较小的数慢慢往前排。对于待排序的数据,每一趟相邻的元素的对比,最后都有一个最值元素移动列末尾。
冒泡排序

核心思想

  1. 比较相邻的元素。如果前一个元素比后一个元素大,就交换这两个元素的位置;
  2. 对每一对相邻元素做同样的工作,从开始第一对元素到结尾的最后一对元素。最终最后位置的元素就是最大值;

算法模拟

  1. 选中的元素为每一趟要进行排序的元素,比较相邻的元素。如果前一个元素比后一个元素大,就交换这两个元素的位置,每经一趟比较,都有一个最值排到最后,标识为红色;
  2. 循环第一步的直到选中的元素个数为1,则完成整个排序过程。
    在这里插入图片描述

程序实现

package com.skystep.algorithm;

public class Bubble {

    public static void sortAsc(Comparable[] comparables) {
        int len = comparables.length;

        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (compare(comparables[j], comparables[j + 1])) {
                    change(comparables, j, j + 1);
                }
            }
        }
    }

    public static void sortDesc(Comparable[] comparables) {
        int len = comparables.length;

        for (int i = 0; i < len - 1; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (!compare(comparables[j], comparables[j + 1])) {
                    change(comparables, j, j + 1);
                }
            }
        }
    }

    public static void change(Comparable[] comparables, int x, int y) {
        Comparable tmp = comparables[x];
        comparables[x] = comparables[y];
        comparables[y] = tmp;
    }

    public static boolean compare(Comparable source, Comparable target) {
        return source.compareTo(target) >= 0;
    }


    public static void main(String[] args) {
        Integer[] arr = {1, 2, 4, 3, 6, 5};
        Bubble.sortDesc(arr);
        for (Integer integer : arr) {
            System.out.print(integer);
            System.out.print(" ");
        }
        System.out.println();

        Bubble.sortAsc(arr);
        for (Integer integer : arr) {
            System.out.print(integer);
            System.out.print(" ");
        }
        System.out.println();

        Student s1 = new Student(10, "s1");
        Student s2 = new Student(13, "s2");
        Student s3 = new Student(12, "s3");
        Student s4 = new Student(17, "s4");
        Student s5 = new Student(14, "s5");
        Student[] students = {s1, s2, s3, s4, s5};

        Bubble.sortDesc(students);
        for (Student student : students) {
            System.out.println("age:" + student.getAge() + "," + "name:" + student.getName());
        }
        System.out.println();

        Bubble.sortAsc(students);
        for (Student student : students) {
            System.out.println("age:" + student.getAge() + "," + "name:" + student.getName());
        }
        System.out.println();
    }
}

适用说明

冒泡法排序的平均时间复杂度是 O(n^2),空间复杂度为常数阶 O(1),具体时间复杂度和数组的有序性也是有关联的。对于少量元素的排序,它是一个有效的算法。

冒泡法排序的最好情况是数据元素集合已经全部排好序,这是循环n-1次每次没有交换动作而退出,因此,冒泡排序的最好情况的时间复杂是是O(n);冒泡排序算法的最坏情况是数据元素集合全部逆序存放,这时循环n-1次 比较次数你n(n-1)/2 和交换移动次数 3n(n-1)/2,因此冒泡排序算法最坏情况时间复杂度为O(n^2)。

选择排序

算法解释

选择排序是一种简单直观的排序算法。一旦涉及排序。大多数实现者首先想到的是选择排序。该排序每趟都假想首个元素为最值,和剩余的元素进行比较,如果发现有比该值小的则交互下标,直到该趟结束,则交换下标对应的值。

核心思想

  1. 每一次遍历的过程中,都假定第一个索引处的元素是最小值,和其他索引处的值依次进行比较,如果当前索引处的值大于其他某个索引处的值,则假定其他某个索引出的值为最小值,最后可以找到最小值所在的索引;
  2. 交换第一个索引处和最小值所在的索引处的值。

算法模拟

在这里插入图片描述

程序实现

package com.skystep.algorithm;

public class Selection {

    public static void sortAsc(Comparable[] comparables) {
        int len = comparables.length;
        for (int i = 0; i < len - 1; i++) {
            int min = i;
            for (int j = i + 1; j < len; j++) {
                if (compare(comparables[min], comparables[j])) {
                    min = j;
                }
            }
            change(comparables, i, min);
        }
    }

    public static void sortDesc(Comparable[] comparables) {
        int len = comparables.length;
        for (int i = 0; i < len - 1; i++) {
            int max = i;
            for (int j = i + 1; j < len; j++) {
                if (!compare(comparables[max], comparables[j])) {
                    max = j;
                }
            }
            change(comparables, i, max);
        }
    }


    public static void change(Comparable[] comparables, int x, int y) {
        Comparable tmp = comparables[x];
        comparables[x] = comparables[y];
        comparables[y] = tmp;
    }

    public static boolean compare(Comparable source, Comparable target) {
        return source.compareTo(target) >= 0;
    }

    public static void main(String[] args) {
        Integer[] arr = {1, 2, 4, 3, 6, 5};
        Selection.sortDesc(arr);
        for (Integer integer : arr) {
            System.out.print(integer);
            System.out.print(" ");
        }
        System.out.println();

        Selection.sortAsc(arr);
        for (Integer integer : arr) {
            System.out.print(integer);
            System.out.print(" ");
        }
        System.out.println();

        Student s1 = new Student(10, "s1");
        Student s2 = new Student(13, "s2");
        Student s3 = new Student(12, "s3");
        Student s4 = new Student(17, "s4");
        Student s5 = new Student(14, "s5");
        Student[] students = {s1, s2, s3, s4, s5};
        Selection.sortDesc(students);
        
        for (Student student : students) {
            System.out.println("age:" + student.getAge() + "," + "name:" + student.getName());
        }
        System.out.println();
        Selection.sortAsc(students);
        for (Student student : students) {
            System.out.println("age:" + student.getAge() + "," + "name:" + student.getName());
        }
        System.out.println();
    }
}

适用说明

选择排序的平均时间复杂度是 O(n^2),空间复杂度为常数阶 O(1),具体时间复杂度和数组的有序性也是有关联的。对于少量元素的排序,它是一个有效的算法。

代码地址

https://gitee.com/yaomianwei2020/study

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值