• 三种简单的排序写下贴上


    void Qsort(int *a,int L,int R) {
        if(L>=R) return ;
        int p=L,q=R;
        int val=a[L];
        while(p!=q) {
            while(p<q&&a[q]>val) q--;
            if(p<q) a[p++]=a[q];
            while(p<q&&a[p]<=val) p++;
            if(p<q) a[q--]=a[p];
        }
        a[p]=val;
        Qsort(a,L,p-1);
        Qsort(a,p+1,R);
    }

    void mergeSort(int *a,int *t,int L,int R) {
        if(L>=R) return ;
        int mid=(L+R)>>1;
        mergeSort(a,t,L,mid);
        mergeSort(a,t,mid+1,R);
        int p=L,q=mid+1,i=L;
        while(p<=mid&&q<=R) {
            t[i++]=a[p]<=a[q]?a[p++]:a[q++];
        }
        while(p<=mid) t[i++]=a[p];
        while(q<=R) t[i++]=a[q];
        for(int i=L;i<=R;i++) a[i]=t[i];
    }

    void heapAdjust(int *a,int pos,int sz) {
        if(pos>sz/2return ;
        int Lchild=pos*2,Rchild=pos*2+1;
        int Min=pos;
        if(Lchild<=sz&&a[Lchild]>a[Min]) {
            Min=Lchild;
        }
        if(Rchild<=sz&&a[Rchild]>a[Min]) {
            Min=Rchild;
        }
        if(Min!=pos) {
            swap(a[pos],a[Min]);
            heapAdjust(a,Min,sz);
        }
    }
    void buildHeap(int *a,int sz) {
        for(int i=sz/2;i>=1;i--) {
            heapAdjust(a,i,sz);
        }
    }
    void HeapSort(int *a,int sz) {
        buildHeap(a,sz);
        for(int i=sz;i>=1;i--) {
            swap(a[1],a[i]);
            heapAdjust(a,1,i-1);
        }
    }
  • 相关阅读:
    神奇的flex布局
    reset、revert、rebase
    Vue.filter过滤器
    moment.js时间格式化总结
    Vue之组件大全
    过滤器filter
    Vue之animate
    Vue之axios
    Mac OS系统上测试PHP代码前的准备工作 | 使用XAMPP搭建Apache服务器的步骤
    Python中的标识符、关键字、变量、语句、注释、模块
  • 原文地址:https://www.cnblogs.com/acvc/p/4645463.html
Copyright © 2020-2023  润新知