469,631 Members | 1,173 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,631 developers. It's quick & easy.

dropping lowest value after an ascending sort

sonic
40
Does anyone know what I can do to this function to get it to drop the lowest value? Thanks for any insight.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. void sort(double* score, int size)
  4. {
  5.     int startScan;
  6.     int minIndex;
  7.     double minValue;
  8.  
  9.     for (startScan = 0; startScan < (size - 1); startScan++)
  10.     {
  11.         minIndex = startScan;
  12.         minValue = score[startScan];
  13.         for (int index = startScan + 1; index < size; index++)
  14.         {
  15.             if (score[index] < minValue)
  16.             {
  17.                 minValue = score[index];
  18.                 minIndex = index;
  19.             }
  20.         }
  21.         score[minIndex] = score[startScan];
  22.         score[startScan] = minValue;
  23.     }
  24. }
  25.  
  26.  
Feb 6 '07 #1
2 1839
horace1
1,510 Expert 1GB
at the end of your sort you could shuffle the elements down one to remove the minimum value, e.g.
Expand|Select|Wrap|Line Numbers
  1.     for (startScan = 0; startScan < (size - 1); startScan++)
  2.         score[startScan]=score[startScan+1];
  3.     score[size-1]=0;
  4.  
you have to decide what you will put at the end to replace the last element moved, in this case I put a 0
Feb 6 '07 #2
Ganon11
3,652 Expert 2GB
Since you are sorting from lowest to highest, after you are finished the sort, increment the pointer by one value. This will leave the lowest value in memory (in the location just before the current pointer), but the array will now start with the second lowest value. If you want to avoid memory leak, you can define a new pointer pointing to the same value as the original pointer, increment the original pointer, and then delete the value stored in the new pointer.
Feb 6 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

17 posts views Thread by rhitz1218 | last post: by
4 posts views Thread by tomek milewski | last post: by
30 posts views Thread by =?Utf-8?B?VGhvbWFzVDIy?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.