Click here to Skip to main content
15,885,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have an array that I ordered it in ascending order. so its ordered correctly.

My code:
float temp; int x; 
    for (int i = 0; i < 4344 ; i++)
    {
       for (int j = i + 1; j < 4344; j++)
       {   
           if (array_dist[i] > array_dist[j])
           {
                temp = array_dist[i];
                array_dist[i] = array_dist[j];
                array_dist[j] = temp;
                x = index_arr[i];
                index_arr[i] = index_arr[j];
             index_arr[j] = x;
           }        
        }
    }
        
    cout << "print out distance colum after ascending sorting : \n";
     
        for (int i = 0; i < 4344; ++i)
     {
         cout << index_arr[i] << " : "
             << array_dist[i] << endl;
     }


What I have tried:

But, I tried to follow the same ordering procedure to store the indices of the original array. But gave me wrong indices. Any suggestion plz.
Posted
Updated 30-Aug-21 0:20am
Comments
Richard MacCutchan 27-Aug-21 10:44am    
I just tried that with some random values and it appears to work correctly. You need to provide more details.
prother123 27-Aug-21 13:02pm    
Actually, it returns the indices in wrong way.
Richard MacCutchan 28-Aug-21 3:28am    
I have no idea what that means, since you have not explained what actual results you get and why they are wrong. In all my tests the indices are correct (as far as I understand the issue) when they are returned.
Greg Utas 27-Aug-21 11:06am    
The code shown looks fine, so did you remember to initialize each entry's index?
prother123 27-Aug-21 13:03pm    
the index_arr has values from 0 ....4343

1 solution

Start by doing a few things:
1) As Rick said, don't use "magic numbers" to control your app - either use #define to declare it as a constant and use that throughout, or pass it as a parameter to the function along with the two arrays to reorder.
2) Reduce the array size to 5 elements, and sort them: then check the indexes array to see if they get scrambled or are correct. If they are fine, change the data until you come up with a version that shows the problem you are having with the "full version".
2.1) If you can't duplicate the problem in small scale, check that your diagnosis that the sort function is wrong - perhaps it's the way you are checking the indexes before and / or after the sort that is at fault?
2.2) If you can duplicate the problem in small scale, use the debugger to follow your code while it is running and see exactly what is happening and where it goes wrong. That should give you clues as to why.

Just saying "it don't work" doesn't help anybody, particularly since we can't check your code with your data!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900