Results 1 to 14 of 14

Thread: Min and Max value in array

  1. #1
    Join Date
    Dec 2007
    Posts
    36
    Thanks
    2
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Min and Max value in array

    Hi all,

    I have an array of 200 int elements which can have positive and negative values. What would be the best way to determine min and max value?

    Regards,
    Benjamin

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Min and Max value in array

    array? pure c++ or an qt container?

    have a look at qMin, qMax (with a loop) or qSort:
    void qSort ( RandomAccessIterator begin, RandomAccessIterator end )
    Sorts the items in range [begin, end) in ascending order using the quicksort algorithm.
    Example:
    Qt Code:
    1. QList<int> list;
    2. list << 33 << 12 << 68 << 6 << 12;
    3. qSort(list.begin(), list.end());
    4. // list: [ 6, 12, 12, 33, 68 ]
    To copy to clipboard, switch view to plain text mode 
    ...

  3. #3
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Min and Max value in array

    Hi,

    Not difficult to implement
    Qt Code:
    1. int iMax = array[0]; //set min and max as the first element
    2. int iMin = array[0];
    3. for (int i=1; i<arraySize; i++)
    4. {
    5. if (array[i] < iMin)
    6. iMin = array[i];
    7. if (array[i] > iMax)
    8. iMax = array[i];
    9. }
    To copy to clipboard, switch view to plain text mode 

    About using qSort and then getting the first and the last values is a good solution if speed is not an issue. Sort algorithms take its time
    Òscar Llarch i Galán

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Min and Max value in array

    Sort algorithms take its time
    Yepp, that's right. And if time really matters is that quicker?
    Qt Code:
    1. int iMax = array[0]; //set min and max as the first element
    2. int iMin = array[0];
    3. for (int i=1; i<arraySize; i++)
    4. {
    5. if (array[i] < iMin)
    6. iMin = array[i];
    7. else
    8. {
    9. if (array[i] > iMax)
    10. iMax = array[i];
    11. }
    12. }
    To copy to clipboard, switch view to plain text mode 
    Because under circumstances (if array[i] < iMin) the second test isn't performed. If so the question of the day remains: Which is quicker?

    Qt Code:
    1. int iMax = array[0]; //set min and max as the first element
    2. int iMin = array[0];
    3. for (int i=1; i<arraySize; i++)
    4. {
    5. if (array[i] < iMin)
    6. iMin = array[i];
    7. else
    8. if (array[i] > iMax)
    9. iMax = array[i];
    10. }
    To copy to clipboard, switch view to plain text mode 

    or

    Qt Code:
    1. int iMax = array[0]; //set min and max as the first element
    2. int iMin = array[0];
    3. for (int i=1; i<arraySize; i++)
    4. {
    5. if (array[i] > iMax)
    6. iMax = array[i];
    7. else
    8. if (array[i] < iMin)
    9. iMin = array[i];
    10. }
    To copy to clipboard, switch view to plain text mode 


  5. #5
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Min and Max value in array

    Hi,

    Seems that you are angry because I just pointed that the sort algorithm is slow to just take the min and max values.

    In my code the 2 "if" statments are converted to 2 assembler instructions. So every iteration it gets 2 instructions to determine if have to assign the value.

    In your code, "if" and "else" are converted to 2 assembler instructions, and then, if the "else" is executed, you have another "if" that is another asm instruction.

    So i really depens on the array values because depending on them my code will execute more instructions than your code and maybe your code will be faster. It only depends on array values. Think on the worst case that every next array value is grater that iMax, then your code performs more instructions.

    Òscar Llarch i Galán

  6. #6
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Min and Max value in array

    Quote Originally Posted by ^NyAw^ View Post
    Seems that you are angry...
    No, sorry, don't get me wrong: I'm not so personally engaged in these things that I can get angry about it.

    In my code the 2 "if" statments are converted to 2 assembler instructions. So every iteration it gets 2 instructions to determine if have to assign the value.

    In your code, "if" and "else" are converted to 2 assembler instructions, and then, if the "else" is executed, you have another "if" that is another asm instruction.
    And this was a real question of me if the code would be faster, which I now know it isn't. (in neither way.)

    Thanks

  7. #7
    Join Date
    Dec 2007
    Posts
    36
    Thanks
    2
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Min and Max value in array

    Thank you all for your answers.

  8. #8
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Min and Max value in array

    Hi,

    And this was a real question of me if the code would be faster, which I now know it isn't. (in neither way.)
    Think on the worst case that every next array value is grater than iMax, then your code performs more instructions.
    And think on the other case that every next array value is minor than iMin, then my code perform more instructions.

    Speaking on order complexity, the two algorithms are O(n), and this is the way that an algorithm speed is calculated because you don't know the values before the code is executed.

    The Quicksort algorithm is O(n logn) as mean, but O(n2) on worst and O(n logn) on best that is greatter than O(n).
    Òscar Llarch i Galán

  9. #9
    Join Date
    Dec 2007
    Posts
    36
    Thanks
    2
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Min and Max value in array

    I have used qMin and qMax functions.

    Is there a way to determine at which position is the max or min value?

  10. #10
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Min and Max value in array

    For finding the max and min the loop from ^NyAw^ should in your case be the best choice. (Then you have also the index, but be aware if there are two or more min/max values...)

  11. #11
    Join Date
    Dec 2007
    Posts
    36
    Thanks
    2
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Min and Max value in array

    This is how I did it:

    _maxValue = _dataValues[0];
    _minValue = _dataValues[0];


    for(int i = 0; i<200; i++){
    _maxValue = qMax(_maxValue, _dataValues[i]);
    _minValue = qMin(_minValue, _dataValues[i]);
    }

    How can I know when max or min value is found, so that I can use index.

  12. #12
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Min and Max value in array

    Quote Originally Posted by Benjamin View Post
    for(int i = 0; i<200; i++)
    Hard coded size is dangerous!

    Qt Code:
    1. _maxValue = _dataValues[0];
    2. _minValue = _dataValues[0];
    3. int _minValueIndex = 0;
    4. int _maxValueIndex = 0;
    5. for (int i=1; i<_dataValues.size(); i++)
    6. {
    7. if (_dataValues[i] < _minValue)
    8. {
    9. _minValue = _dataValues[i];
    10. _minValueIndex = i;
    11. }
    12. if (_dataValues[i] > _maxValue)
    13. {
    14. _maxValue = _dataValues[i];
    15. _maxValueIndex = i;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 

    (WITHOUT catching the case if there are two/more min/max values!)

  13. #13
    Join Date
    Dec 2007
    Posts
    36
    Thanks
    2
    Qt products
    Qt3 Qt/Embedded
    Platforms
    Unix/X11

    Default Re: Min and Max value in array

    Thank you for your replay.

    I am wondering one thing, can this way of finding min and max values work with mixed positive and negative numbers?

  14. #14
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Min and Max value in array

    Quote Originally Posted by Benjamin View Post
    I am wondering one thing, can this way of finding min and max values work with mixed positive and negative numbers?
    Why not? (-1 > 2) or (2 > -1) are fine...

  15. The following user says thank you to Lykurg for this useful post:

    Benjamin (27th February 2009)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.