zoukankan      html  css  js  c++  java
  • 排序源码(待续)

    #include <stdio.h>
    void exchange(int &a,int &b){
        a = a^b;
        b = a^b;
        a = a^b;
    }

    void printout(int a[],int n){
        for(int i=0;i<n;i++){
            printf("%5d ",a[i]);
        }
    }

    //直接选择排序
    void selectSort(int a[],int n){
        int i,j;
        for(i=0;i<n;i++){
            for(j=i+1;j<n;j++){
                if(a[i]>a[j]){
                    exchange(a[i],a[j]);
                }
            }
        }
        printout(a,n);
    }

    //冒泡排序
    void bubbleSort(int a[],int n){
        int i,j;
        for(i=0;i<n-1;i++){
            for(j=0;j<n-i-1;j++){
                if(a[j]>a[j+1]){
                    exchange(a[j],a[j+1]);
                }
            }

        }
        printout(a,n);
    }






    //建大顶堆
    void heapAdjust(int a[],int s,int l){
        int tag = s; //保存当前正在比较的父节点
        for(int j=2*s+1;j<=l;j = 2*j+1){
            if(j<l && a[j]<a[j+1]){
                j++;
            }
            if(a[tag] < a[j]){
                exchange(a[tag],a[j]);
                tag = j;
            }else{
                break;
            }
        }

    }

    //堆排序
    void heapSort(int a[],int n){
        int i;
        //建大顶堆
        for(i=((n-2)>>1);i>=0;i--){
            heapAdjust(a,i,n-1);
        }

        //进行堆排序,并每次将堆节点放到最后一位,即将当前堆最大值放到最后
        for(i=n-1;i>0;--i){
            exchange(a[0],a[i]);
            heapAdjust(a,0,i-1);
        }
        printout(a,n);
    }

    //快速排序
    void subSort(int a[],int low,int high){

        int temp = a[low];
        int i=low,j=high;
        if(i<j){

        while(true){
            while(i < j && a[j]>=temp){
                j--;
            }
            while(i < j && a[i]<=temp){
                i++;
            }
            if(i<j)
                exchange(a[i],a[j]);
            else
                break;
        }

        exchange(a[low],a[i]);

        subSort(a,low,i-1);
        subSort(a,i+1,high);

        }
    }

    void quickSort(int a[],int n){
        subSort(a,0,n-1);
        printout(a,n);
    }

    int main()
    {
        int a[] ={3,1,2,9,5};
       // selectSort(a,3);
        printout(a,5);
        printf("\ntest:");
        //bubbleSort(a,3);
        // heapSort(a,6);
        quickSort(a,5);
        return 0;
    }

  • 相关阅读:
    关于RAS加密中pfx格式提取字符串私钥 (转)
    Oracle冷备份和热备份的实践(原创)
    数据库的备份与恢复(oracle 11g) (转)
    Oracle安装-------实例化EM 配置文件时出错问题 ( 转 )
    oracle表空间维护常用命令
    使用Navicat for Oracle新建表空间、用户及权限赋予 (转)
    【JavaScript】JavaScript模块化编程
    【随笔】入行必读:互联网行业薪酬等级
    【JavaScript】JavaScript Promise 探微
    【Java】关于并发
  • 原文地址:https://www.cnblogs.com/mapleyuan/p/3017628.html
Copyright © 2011-2022 走看看