【问题标题】:Problem with sorting array algorithm排序数组算法的问题
【发布时间】:2011-04-18 16:45:05
【问题描述】:

我有三个数组。我正试图按其中一个对所有这些进行排序。所以我的数组是 itemarray、pricearray、quantityarray。我希望对 itemarray 进行排序,但相应的数组没有与 itemarray 一起正确排序。

这是我创建的算法。你知道我该如何解决这个问题吗??

DO i=1, NumItems-1

    SmallestItem = MINVAL(itemarray(i:NumItems))
    MINLOC_array = MINLOC(itemarray(i:NumItems))
    Locationsmallest = (i-1)+MINLOC_array(1)

    itemarray(Locationsmallest) = itemarray(i)
    itemarray(i) = SmallestItem

    pricearray(Locationsmallest) = pricearray(i)
    pricearray(i) = SmallestItem

    quantityarray(Locationsmallest) = quantityarray(i)
    quantityarray(i) = SmallestItem

END DO  

【问题讨论】:

    标签: arrays fortran sorting fortran95


    【解决方案1】:

    您将 pricearray(i) 设置为来自 itemarray 的内容。您应该交换pricearray(Locationsmallest)pricearray(i),您可以通过将pricearray(Locationsmallest) 的值存储在一个临时变量中来实现。

    quantityarray(i) 也是如此。

    顺便说一下,这是一个 O(n^2) 算法,当你的数组中有大量值时可能会很慢。

    【讨论】:

    • 临时变量应该是这样的:pricearray(locationsmallest)=pricesmallestitem 我在逻辑上有困难。
    • @EuropaDust:类似:temp = pricearray(Locationsmallest); pricearray(位置最小) = pricearray(i);价格数组(i)= 温度。 (用换行符替换分号。)但我承认我不知道 Fortran 语法,所以你可能需要稍微调整一下
    • fortran 实际上非常好,即使包含分号。
    猜你喜欢
    • 1970-01-01
    • 2022-06-10
    • 1970-01-01
    • 2011-01-11
    • 2019-10-11
    • 2017-01-14
    • 1970-01-01
    • 2011-09-27
    相关资源
    最近更新 更多