Sorting two arrays (parallel) question.

When I run my full program, array1 sorts perfectly. However, the values that go along with array1, which are stored in the same location in array2, don't change to the same location.

Does that make sense?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1)//Sorts the arrays from highest to lowest
//, based on ID number
{
		
	    int i, j, pos;
	    int buff[MAX_NUM];
		string temp;
		int temp2;
		
      	for (i = count1-1; i > 0; i--)
     	{
           pos = 0;                 // initialize to subscript of first element
           for (j = 0; j <= i; j++)   // locate smallest between positions 1 and i.
           {
                 if (array1[j] < array1[pos])
                 	pos = j;
           }
           temp = array1[pos];   // Swap smallest found with element in position i.
           temp2 = buff[pos];
           array1[pos] = array1[i];
           buff[pos] = buff[i];
           array1[i] = temp;
           buff[pos] = temp2;
		}
		array2 = buff;
}
Last edited on
Should,
buff[pos] = temp2;
perhaps be,
buff[i] = temp2;
Yes, it should be. But unfortunately that didn't change anything. I also have this way of doing it, but it sorts it in the opposite order than it needs to be in, but the data does stay together.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1)//Sorts the arrays from highest to lowest
//, based on ID number
{
	
  int curTop,            // Current top of unsorted list
      tryIdx,            // Position to compare value to
      minPosition;       // Position of smallest value
   string temp;      
   int temp2;        // Temp value for swapping

  // for each item in the list (top to bottom)
  for (curTop = 0; curTop < count1 - 1; curTop++) 
  {
          
     minPosition = curTop;      // start with current top as smallest value

     // find smallest value from curTop down
     for (tryIdx = curTop + 1; tryIdx < count1; tryIdx++) 
         if (array1[tryIdx] < array1[minPosition])
             minPosition = tryIdx;
				
     // if smallest not at curTop, swap with curTop
     if (minPosition != curTop) 
     {
         temp = array1[curTop];
         temp2 = array2[curTop];
         array1[curTop] = array1[minPosition];
         array2[curTop] = array2[minPosition];

         array1[minPosition] = temp;
         array2[minPosition] = temp2;
         
     }   // end swap

  }  // end for
 
  return;
}
What happens if, in the second code you say works, you change,
if (minPosition != curTop)
to,
if (minPosition == curTop)
?

Without understanding what the data actually is and/or the actual sorting task required it makes it difficult to figure out what the problem might be.
Last edited on
Topic archived. No new replies allowed.