数组排序算法的问题
我有三个数组。我正在尝试按其中之一对所有这些进行排序。所以我的数组是 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
I have three arrays. And I am trying to sort all of them by one of them so. So my arrays are itemarray, pricearray, quantityarray. I want itemarray to be sorted but the corresponding arrays aren't sorting appropriately along with itemarray.
Here is the algorithm I created. Do you know how I can fix this??
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您将
pricearray(i)
设置为来自itemarray
的内容。您应该交换pricearray(Locationsmallest)
和pricearray(i)
,可以通过将pricearray(Locationsmallest)
的值存储在临时变量。quantityarray(i)
也是如此。顺便说一句,这是一个 O(n^2) 算法,当数组中有大量值时,它可能会非常慢。
You are setting
pricearray(i)
to something that came fromitemarray
. You should be swappingpricearray(Locationsmallest)
andpricearray(i)
, which you can do by storing the value ofpricearray(Locationsmallest)
in a temporary variable.The same is true for
quantityarray(i)
.By the way, this is an O(n^2) algorithm, and is likely to be very slow when there are a large number of values in your array.