• 排序算法(堆排序)


    堆排序是对选择排序的改进(时间复杂度和希尔排序一样O(nlog2n))

    数据结构:完全二叉树(大顶堆,根节点都比左右节点大,小顶堆,根节点小于双亲节点)

    public class HeapSort {
        public static void main(String[] args) {
            int a[]={1,2,4,9,33,2,6,7,10,11};
            sort(a, 10);
            for (int i = 0; i < a.length; i++) {
                System.out.print(a[i]+" ");
            }
            
        }
        
        //调整堆
        public static void heapAdjust(int a[],int s,int n){
            
            int i,temp;
            temp=a[s];
            
            for(i=2*s;i<n;i*=2){
                if(i+1<n&&a[i]<a[i+1]){
                    i++;
                }
                
                if(temp>=a[i]) break;
                
                a[s]=a[i];
                s=i;
            }
            a[s]=temp;
        }
        
        //交换位置
        public static void swap(int a[],int i,int j){
            int temp;
            temp=a[i];
            a[i]=a[j];
            a[j]=temp;
        }
        
        //排序
        public static void sort(int h[],int n){
            int i;
            for(i=n/2;i>0;i--){
                heapAdjust(h, i, n);
            }
            
            for(i=n-1;i>1;i--){
                swap(h, 1, i);
                heapAdjust(h, 1, i-1);
            }
        }
    }

  • 相关阅读:
    HTTP请求方法 GET POST【总结】
    Data Binding MVVM 数据绑定 总结
    Butter Knife 黄油刀
    原生JS强大DOM选择器querySelector与querySelectorAll
    JS日期Date详解与实例扩展
    JS中的冒泡排序以及实现一个数组中得最到最大的数字小例
    jquery.roundabout.js实现3D图片层叠旋转木马切换
    AngularJS 表格
    AngularJS XMLHttpRequest
    AngularJS Filters
  • 原文地址:https://www.cnblogs.com/LvLoveYuForever/p/5744317.html
Copyright © 2020-2023  润新知