码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2015-02-09 21:26:03      阅读:373      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

技术分享

 

技术分享

 

#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}/*

 

 

技术分享技术分享技术分享技术分享技术分享

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

标签:

原文地址:http://www.cnblogs.com/sunyongjie1984/p/4282365.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!