数据结构排序算法---堆排序

简介: 数据结构排序算法---堆排序

正文


堆排序:主要是构建堆以及调整堆;

时间复杂度:o(n*logn)

调整分为向上调整与向下调整;

下面是实现的代码:小根堆;


/**
 * 堆排序
 *
 * @author :breakpoint/赵立刚
 * @date : 2020/07/09
 */
public class HeapSortTest {
    /*
        heap sort 的核心就是调整我们的堆
     */
    public static void main(String[] args) {
        int[] nums = {1, 2, 4, 5, 9, 6, 7, 3, 8,-1};
        new HeapSortTest().hSort(nums);
    }
    private void hSort(int[] nums) {
        for (int i = nums.length, pos = nums.length; i >= 1; i--) {
            upAdjust(nums, pos);
            System.out.println(nums[0]);
            nums[0] = nums[i - 1];
            pos--;
        }
    }
    /**
     * 调整为小根堆
     *
     * @param nums 数值
     * @param pos  最后的位置
     */
    private void upAdjust(int[] nums, int pos) {
        int index = pos / 2;
        // 进行调整
        while (index >= 1) {
            int swapIndex = -1;
            if ((swapIndex = getSwapIndex(nums, index, pos)) != -1) {
                if (nums[index - 1] > nums[swapIndex - 1]) {
                    int temp = nums[index - 1];
                    nums[index - 1] = nums[swapIndex - 1];
                    nums[swapIndex - 1] = temp;
                }
            }
            index--;
        }
    }
    // 获取交换位置
    private int getSwapIndex(int[] nums, int index, int pos) {
        int swapIndex = -1;
        if (2 * index + 1 <= pos) {
            swapIndex = nums[2 * index] > nums[2 * index - 1] ?
                    2 * index : 2 * index + 1;
        } else if (2 * index <= pos) {
            swapIndex = 2 * index;
        }
        return swapIndex;
    }
    // 向下调整  暂时没有用到
    @Deprecated
    private void downAdjust(int[] nums, int pos) {
        int num = nums[0];
        int swapIndex = 0;
        int index = 1;
        while (swapIndex != -1) {
            if ((swapIndex = getSwapIndex(nums, index, pos)) != -1) {
                if (nums[index - 1] > nums[swapIndex - 1]) {
                    int temp = nums[index - 1];
                    nums[index - 1] = nums[swapIndex - 1];
                    nums[swapIndex - 1] = temp;
                }
            }
            index = swapIndex;
        }
        nums[index - 1] = num;
    }
}


运行结果:

6.png

相关文章
|
14天前
|
机器学习/深度学习 算法 存储
[数据结构]——算法的时间复杂度和空间复杂度
[数据结构]——算法的时间复杂度和空间复杂度
|
21天前
|
存储 监控 NoSQL
Redis处理大量数据主要依赖于其内存存储结构、高效的数据结构和算法,以及一系列的优化策略
【5月更文挑战第15天】Redis处理大量数据依赖内存存储、高效数据结构和优化策略。选择合适的数据结构、利用批量操作减少网络开销、控制批量大小、使用Redis Cluster进行分布式存储、优化内存使用及监控调优是关键。通过这些方法,Redis能有效处理大量数据并保持高性能。
39 0
|
7天前
|
算法
重拾数据结构和算法——脑图
重拾数据结构和算法——脑图
13 0
|
12天前
|
算法 搜索推荐 Java
Java数据结构 -- 常见算法分析(查找算法、排序算法)精解详解!!!
Java数据结构 -- 常见算法分析(查找算法、排序算法)精解详解!!!
7 0
|
13天前
|
存储 搜索推荐 算法
C语言数据结构算法,常用10种排序实战
插入排序(Insertion Sort) 希尔排序(Shell Sort) 选择排序(Selection Sort) 冒泡排序(Bubble Sort) 归并排序(Merge Sort) 快速排序(Quick Sort) 堆排序(Heap Sort) 基数排序(Radix Sort)
11 1
C语言数据结构算法,常用10种排序实战
|
13天前
|
存储 搜索推荐 算法
[数据结构]————排序总结——插入排序(直接排序和希尔排序)—选择排序(选择排序和堆排序)-交换排序(冒泡排序和快速排序)—归并排序(归并排序)
[数据结构]————排序总结——插入排序(直接排序和希尔排序)—选择排序(选择排序和堆排序)-交换排序(冒泡排序和快速排序)—归并排序(归并排序)
|
13天前
|
算法
[数据结构]——二叉树——堆排序
[数据结构]——二叉树——堆排序
|
15天前
|
算法 搜索推荐
数据结构与算法⑫(第四章_中_续一)堆排序(详解)
数据结构与算法⑫(第四章_中_续一)堆排序(详解)
24 0
|
19天前
|
缓存 算法 Java
数据结构~缓存淘汰算法--LRU算法(Java的俩种实现方式,万字解析
数据结构~缓存淘汰算法--LRU算法(Java的俩种实现方式,万字解析
|
21天前
|
机器学习/深度学习 算法 数据可视化
Python 数据结构和算法实用指南(四)(4)
Python 数据结构和算法实用指南(四)
30 1