Problem with sorting array algorithm
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
You are setting pricearray(i)
to something that came from itemarray
. You should be swapping pricearray(Locationsmallest)
and pricearray(i)
, which you can do by storing the value of pricearray(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.
精彩评论