# Thread: Finding the smallest value......HARD!!!

1. ## Finding the smallest value......HARD!!!

To find the smallest value in a number of values may seem easy, but I think it is not that easy after all.

I've an array of float values. (Stored in Values[j])

I'm supposed to find the smallest value among all the values. After, finding the smallest value, I''m to find the second smallest value, next the third smallest value....etc

Code:
```j=0;
Smallest = Value[0];
for (j=0; j< MaxArrayValue ; j++){
if (Value[j+1] < Smallest ){
Smallest = Value [j+1];
}//End If
}//End For```
In the above codes, I'm able to get the smalest values....but to find the second smallest values and rest of the values will not be easy at all......

Note: I'm not arranging the values in ascending order, I'm getting the smallest values out so that I could perform some calculations without affecting the "already selected smallest value".

_____

2. It'll be much easier if you sort the array (by implementing some sorting algo) and then you know where your greatest and least numbers are stored.

3. I cannot sort the values.......that is why I'm so traumatized and being constantly haunted by this...........

If I sort the values, I'll not be able to proceed on with the coding.....

_____

4. so why dont you copy the array in a "buffer" array, sort it and use it....and for your program keep using the "real" not sorted one?

If I calculate the codes 10 times, I must copy the codes 10 times...

If I calculate the codes n times, I must copy the codes n times...
How am I going to create n number of variables to copy the values of the array..??

_____

6. Maybe you should explain what the actual goal of this thing is, because I'm not following the problem at all.

7. Why is this hard? You first find the smallest value. Then you say you're stuck on finding the "second smallest"? Well how do you yourself pick out the "second smallest" from a list? I'll tell you:

First you find the smallest, and make a note of it.
Then you go through the list and find the smallest of those, that is bigger than the one you made a note of.

See, it's not hard at all.

Quzah.

8. ## Simple solution

1) Build a index array, filled with values from 0 to whatever your max index is

2) Sort the array, using the index number to dereference the value. (ie you'll only be changing the values in the index array, the float array order remains unchanged). Use whatever sort routine suits your fancy.

3) Step through the index array, fetching values from your float array.

Simple enough?

9. Here's an easier way: Keep track of both lowest and second-lowest on the first pass through the array. Faster than sorting can ever hope to be. Also much easier with much less code. One single for loop and a run through the array does it all.

Quzah.

10. Quzah:

Yes, but what if there are several duplicate values...

11. Originally Posted by AssistMe
I'm supposed to find the smallest value among all the values. After, finding the smallest value, I''m to find the second smallest value, next the third smallest value....etc
Was there something here you didn't quite understand?

12. Originally Posted by Dave_Sinkula
Was there something here you didn't quite understand?
Nope.....I understand the sorting...but the thing I'm asking here is different from that.....I've X number of features....and after each set of calculations, I'm supposed to find the smallest calculated values of each features......In the next round of calculations, I'm supposed to ommit the selected features and find the smallest values again.....(in other words, find next samllest value.....)

_____

13. Ok, this is getting silly. Here is what you want. Just create two same sized arrays, one with the float values, the other ints initialized to 0. Then repeatedly call this function:

Code:
```int FindSmallest(float* Value, int* doneArray, int MaxArrayValue)
{
int j;
int Smallest = -1;

for (j = 0; j < MaxArrayValue; j++)
{
if (!doneArray[j])
{
if ((Smallest == -1) || (Value[j] < Value[Smallest]))
{
Smallest = j;
}//End If
}// end if doneArray
}//End For
if (Smallest != -1)
doneArray[Smallest] = 1;
return (Smallest);
}```
Hope that helps.
(code is untested/off the top of my head. Caveat Emptor!)

The Peachinator

14. Frankly, that's a stupid way to do it. Do what I said in the first place:
Code:
```int x;
int smallest = INT_MAX, second = INT_MAX;
int smallestindex = -1, secondindex = -1;

for( x = 0; x < number; x++ )
if( array[x] < smallest )
{
second = smallest;
secondindex = smallestindex;

smallest = array[x];
smallestindex = x;
}```
Wow, that was hard.

Quzah.

15. As I said before, your approach fails if there are any duplicate values in the array.