簡體   English   中英

同時最小和最大

[英]Simultaneous minimum and maximum

我試圖實現一種算法,在給定的數組中搜索最小和最大元素,並使用Cormen的算法導論中的想法。 我的代碼編譯並開始工作,輸出生成的隨機數組,然后在很長一段時間內什么都不做。 為什么會這樣?

代碼是這樣的:

// fast min and max --cormen exercise 1.cpp: entry point
//implemented from a verbal description in cormen's book, p 243

#include "stdafx.h"
#include <vector>
#include <ctime>
#include <cstdlib>
#include <iostream>

struct min_and_max
{
    int min, max;
};


min_and_max find_min_and_max(std::vector<int>& A)
{
    int n = A.size();
    int min, max;
    if (n%2 == 1)
        min = max = A[0];
    if (n%2 == 0)
        if (A[0] < A[1])
        {
            min = A[0];
            max = A[1];
        }
        else
        {
            min = A[1];
            max = A[0];
        }
    for(int i = 2; i < A.size(); (i + 2))
    {
        if (A[i] < A[i+1])
        {
            if (min > A[i])
                min = A[i];
            if (max < A[i+1])
                max = A[i+1];
        }
        else
        {
            if (min > A[i+1])
                min = A[i+1];
            if (max < A[i])
                max = A[i];
        }
    }
    min_and_max result;
    result.min = min;
    result.max = max;

    return result;
}

int main()
{
    std::srand(std::time(0));
    std::vector<int> A(10);
    for (auto i = 0; i < A.size(); i++)
        {
            A[i] = rand() % 1000;
            std::cout << A[i] << " ";           
        }
    std::cout << std::endl; //IT GOES AS FAR AS THIS
    std::cout << "The array has been analyzed; its' minimum is " << find_min_and_max(A).min << "and its' maximum is " << find_min_and_max(A).max << std::endl;

    return 0;
}
 for(int i = 2; i < A.size(); (i + 2))

i + 2不會改變i的值,你需要使用i += 2

問題在於:

for(int i = 2; i < A.size(); (i + 2))

你永遠不會實際增加i ,從而導致無限循環。

將其更改為:

for(int i = 2; i < A.size(); i+=2)

除了給定的答案之外,如果您使用的是c ++ 11,則可以使用lambdasstd::for_each函數簡化算法:

#include <algorithm>
#include <iostream>
#include <cmath>

int main() { 
    int array[] = { -8, 8, 0, 9, 5, -3, 4, 6, -1, 15, 31 };
    int min, max;
    // User std::for_each(v.begin(), v.end(), ...) for either vector or list
    std::for_each(std::begin(array), std::end(array), [&min, &max](int elem) { 
        max = std::max(max, elem);
        min = std::min(min, elem);
    });
    std::cout << min << ", " << max << std::endl;
    return 0;
}

也許它可能更簡單

更新:正如@Blastfurnace指出的那樣, std::minmax_element函數可用於進一步減少搜索min和max元素所需的代碼,從而產生這個更短的版本:

#include <algorithm>
#include <iostream>
#include <vector>   

int main() { 
    std::vector<int> values = { -8, 8, 0, 9, 5, -3, 4, 6, -1, 15, 31 };
    auto minAndMax = std::minmax_element(values.begin(), values.end());
    std::cout << *minAndMax.first << ", " << *minAndMax.second << std::endl;
    return 0;
}

重要的是要注意,除了OT之外,在這個答案中所做的一切都是為了學習,提供OP替代方案來改進他(或她)的工作並幫助其他可能具有相同要求的用戶。

在任何情況下算法都是不正確的,因為向量的大小可以等於0.在這種情況下1)您嘗試訪問不存在的alement和2)您從函數返回未定義的值。 更正確的方法是返回最小和最大元素的索引,如果向量為空,則返回一對A.size()。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM