• 算法导论第9章中位数和顺序统计学


    S1O`QF0HMQBJ~7NPA2`8GEV

    _LTJ7S)QX73[QQDK0M{Y{RL

    Y3PAC4FMNGE`~_Z@PX(BSKO

    #include <iostream>
    #include <stdint.h>
    #ifdef __linux
    #include <stdio.h>
    #endif
    // MINIMUM(A)
    // MIN = A[1]
    // for i = 2 to A.length
    //     if min > A[i]
    //         min = A[i]
    // return min
    
    int64_t minimum(int64_t* A, int64_t n)
    {
        int64_t min = A[0];
        for (int64_t i = 0; i < n; i++)
        {
            if (min > A[i])
            {
                min = A[i];
            }
        }
        return min;
    }
    
    int64_t maximum(int64_t* A, int64_t n)
    {
        int64_t max = A[0];
        for (int64_t i = 0; i < n; i++)
        {
            if (max < A[i])
            {
                max = A[i];
            }
        }
        return max;
    }
    
    class MaxMin {
    public:
        int64_t min;
        int64_t max;
    };
    
    MaxMin maxminmum(int64_t* A, int64_t n)
    {
        MaxMin maxmin;
        if (0 == n % 2)
        {
            if (A[0] > A[1])
            {
                maxmin.min = A[1];
                maxmin.max = A[0];
            }
            else
            {
                maxmin.min = A[0];
                maxmin.max = A[1];
            }
            for (int64_t i = 2; i < n - 1; i+=2)
            {
                if (A[i] > A[i + 1])
                {
                    if (A[i] > maxmin.max)
                    {
                        maxmin.max = A[i];
                    }
                    if (A[i + 1] < maxmin.min)
                    {
                        maxmin.min = A[i + 1];
                    }
                }
                else
                {
                    if (A[i + 1] > maxmin.max)
                    {
                        maxmin.max = A[i + 1];
                    }
                    if (A[i] < maxmin.min)
                    {
                        maxmin.min = A[i];
                    }
                }
            }
        }
        else
        {
            maxmin.max = maxmin.min = A[0];
            for (int64_t i = 1; i < n - 1; i+=2)
            {
                if (A[i] > A[i + 1])
                {
                    if (A[i] > maxmin.max)
                    {
                        maxmin.max = A[i];
                    }
                    if (A[i + 1] < maxmin.min)
                    {
                        maxmin.min = A[i + 1];
                    }
                }
                else
                {
                    if (A[i + 1] > maxmin.max)
                    {
                        maxmin.max = A[i + 1];
                    }
                    if (A[i] < maxmin.min)
                    {
                        maxmin.min = A[i];
                    }
                }
            }
    
        }
        return maxmin;
    }
    
    int main()
    {
        // even number array
        int64_t array0[6] = { 5, 2, 4, 6, 1, 3 };
        int64_t min = minimum(array0, sizeof(array0) / sizeof(int64_t));
        std::cout << min << std::endl;
        int64_t max = maximum(array0, sizeof(array0) / sizeof(int64_t));
        std::cout << max << std::endl;
    
        MaxMin maxmin = maxminmum(array0, sizeof(array0) / sizeof(int64_t));
        std::cout << maxmin.min << std::endl;
        std::cout << maxmin.max << std::endl;
    
        // not even number array
        int64_t array1[7] = { 5, 2, 4, 6, 1, 3, 10 };
        min = minimum(array1, sizeof(array1) / sizeof(int64_t));
        std::cout << min << std::endl;
        max = maximum(array1, sizeof(array1) / sizeof(int64_t));
        std::cout << max << std::endl;
    
        maxmin = maxminmum(array1, sizeof(array1) / sizeof(int64_t));
        std::cout << maxmin.min << std::endl;
        std::cout << maxmin.max << std::endl;
    
        getchar();
        return 0;
    }
    
    BIN     = bin
    FMCSA   = find_max_crossing_subarray
    IAS     = IA_solution
    IST     = insertion_sort_t
    LMSA    = liner_time_max_subarray
    MERGE   = merge
    MERGE_T = merge_t
    VMS     = violate_max_subarray
    STRA    = 4.2_strassen
    COUNT_SORT = 8.2_counting_sort
    MINIMUMMAXIMUM = 9.1_MinimumMaximum
    
    CFLAGS = -Wall
    
    all : ${BIN}/${COUNT_SORT} ${BIN}/${FMCSA} ${BIN}/${IAS} ${BIN}/${IST} ${BIN}/${LMSA} ${BIN}/${MERGE} ${BIN}/${MERGE_T} ${BIN}/${VMS} ${BIN}/${STRA} ${BIN}/${MINIMUMMAXIMUM}
    
    ${BIN}/${COUNT_SORT} : ${COUNT_SORT}/counting_sort.cpp
    	g++ ${COUNT_SORT}/counting_sort.cpp ${CFLAGS} -o ${BIN}/${COUNT_SORT}
    
    ${BIN}/${FMCSA} : ${FMCSA}/main.cpp ${FMCSA}/max_sub_array.h
    	g++ ${FMCSA}/main.cpp ${CFLAGS} -o ${BIN}/${FMCSA}
    
    ${BIN}/${IAS} : ${IAS}/insertion_sort.cpp ${IAS}/insertion_sort.h
    	g++ ${IAS}/insertion_sort.cpp ${CFLAGS} -o ${BIN}/${IAS}
    
    ${BIN}/${IST} : ${IST}/${IST}.cpp ${IST}/${IST}.h
    	g++ ${IST}/${IST}.cpp ${CFLAGS} -o ${BIN}/${IST}
    
    ${BIN}/${LMSA} : ${LMSA}/Source.cpp
    	g++ ${LMSA}/Source.cpp ${CFLAGS} -o ${BIN}/${LMSA}
    
    ${BIN}/${MERGE} : ${MERGE}/${MERGE}.cpp  ${MERGE}/${MERGE}.h
    	g++ -std=c++0x ${MERGE}/${MERGE}.cpp ${CFLAGS} -o ${BIN}/${MERGE}
    
    ${BIN}/${MERGE_T} : ${MERGE_T}/${MERGE_T}.cpp  ${MERGE_T}/${MERGE_T}.h
    	g++ -std=c++0x ${MERGE_T}/${MERGE_T}.cpp ${CFLAGS} -o ${BIN}/${MERGE_T}
    
    ${BIN}/${VMS} : ${VMS}/Source.cpp
    	g++ ${VMS}/Source.cpp ${CFLAGS} -o ${BIN}/${VMS}
    
    ${BIN}/${STRA} : ${STRA}/strassen.cpp ${STRA}/strassen.h
    	g++ -std=c++0x ${STRA}/strassen.cpp ${CFLAGS} -o ${BIN}/${STRA}
    
    ${BIN}/${MINIMUMMAXIMUM} : ${MINIMUMMAXIMUM}/${MINIMUMMAXIMUM}.cpp
    	g++ ${MINIMUMMAXIMUM}/${MINIMUMMAXIMUM}.cpp ${CFLAGS} -o ${BIN}/${MINIMUMMAXIMUM}
    clean:
    	rm ${BIN}/*
    

    c9578577-3a88-4cb3-907e-a967c1d8b27adb96e2e2-c182-4384-a18c-fe71aa606bbbgirl3good4IMG_0995

  • 相关阅读:
    log4j在eclipse下使用
    Java实现IOS推送(利用javapns1.6)
    [转]浅谈多核CPU、多线程与并行计算
    关于select的字段加别名的语法
    [转]maven环境快速搭建
    Java实现IOS推送(Javapns2.2)
    千年服务器GM命令及使用详解
    [转载 蓝韦巍]类似浩方、VS对战平台系统的实现
    召唤和删除怪物程序段
    WPE下载 & WPE & 各版本下载 及 教程
  • 原文地址:https://www.cnblogs.com/sunyongjie1984/p/4282365.html
Copyright © 2020-2023  润新知