Java数据结构-选择排序

一.直接选择排序

1.基本思想

每一次从待排序的数据元素中选出最小(或最大)的一个元素,存放在序列的起始位置,直到全部待排序的数据元素排完 。

2.代码实现

1.简单选择排序
public static void selectSort(int[] array) {
        for (int i = 0; i < array.length; i++) {
            int minIndex = i;
            for (int j = i+1; j < array.length; j++) {
                if (array[j] < array[minIndex]) {
                    minIndex = j;
                }
            }
            swap(array, i, minIndex);
        }
}
private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
}
2.双向选择排序
private static void swap(int[] array, int i, int j) {
        int tmp = array[i];
        array[i] = array[j];
        array[j] = tmp;
    }

    public static void selectSort2(int[] array) {
        int left = 0;
        int right = array.length - 1;
        while (left < right) {
            int minIndex = left;
            int maxIndex = left;
            for (int i = left+1; i <= right; i++) {
                if (array[i] < array[minIndex]) {
                    minIndex = i;
                }
                if (array[i] > array[maxIndex]) {
                    maxIndex = i;
                }
            }
            swap(array,left,minIndex);
            // 如果最大值正好是left下标,minIndex和left交换,此时把最大值换到minIndex的位置了
            if (maxIndex == left) { // 判断
                maxIndex = minIndex;
            }
            swap(array,right,maxIndex);
            left++;
            right--;
        }
    }

3.特性总结

1. 直接选择排序效率不是很好。实际中很少使用

2. 时间复杂度:O(N^2)

3. 空间复杂度:O(1)

4. 稳定性:不稳定

二.堆排序

1.基本思想

堆排序(Heapsort)是指利用堆积树(堆)这种数据结构所设计的一种排序算法,它是选择排序的一种。它是通过堆来进行选择数据。需要注意的是排升序要建大堆,排降序建小堆。例如,通过构建大根堆和不断交换元素的方式,将数组中的最大值依次放到数组的末尾,最终实现整个数组的有序排列。

2.代码实现

public static void heapSort(int[] array) {
        createTree(array);
        int end = array.length - 1; //末尾元素下标
        while (end > 0) {
            swap(array, 0, end); //交换首元素和尾元素
            siftDown(array, 0, end); //向下调整为大根堆
            end--;
        }
    }

    private static void createTree(int[] array) {
        // (array.length-1-1) / 2 = (末尾元素下标-1)/2 = 末尾元素父节点的下标
        // parent-- 上一个父节点
        for (int parent = (array.length-1-1) / 2; parent >= 0 ; parent--) {
            siftDown(array,parent,array.length);
        }
    }
    private static void siftDown(int[] array, int parent, int length) { //数组,根节点,结束节点
        int child = 2 * parent + 1;
        while (child < length) {
            //如果存在右孩子且右孩子的值大于左孩子,将child存储下标置为右孩子
            if (child + 1 < length && array[child] < array[child + 1]) {
                child++;
            }
            if (array[child] > array[parent]) {
                swap(array, parent, child); //交换后更新节点位置
                parent = child;
                child = 2 * parent + 1;
            }else {
                break;
            }
        }
    }

3.特性总结

1. 时间复杂度:O(N*logN)

2. 空间复杂度:O(1)

3. 稳定性:不稳定

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值