Link to home
Start Free TrialLog in
Avatar of Embt
Embt

asked on

sorting arrays

Hi, experts:

I'd like to know if there is existing C/C++ code for sorting an array(int, float, or char) in descending order? and more, if it is an int array, I need to get rid of those elements whose value is 0, so the array will only contain non-zero values(the size is shrinking as well).  how do I achieve this? Do I have to use STL?

Thanks in advance.
Avatar of jkr
jkr
Flag of Germany image

The easiest solution - if you don't want to use STL - would be the CRT function 'qsort()'. But STL would make life easier anyway...
Avatar of nietod
nietod

I would use the qsort() as jkr said, and then remove the 0 entries using memmove().   Note that if there are no negative entries, then the 0 entries will occur at the end of the sorted items and there won't be any need to remove the 0 entries, jus ignore them.
The main drawback using STL is that sorting is only implemented for lists and not vectors, thus creating a little bit overhead ;-)
I don't think that is true.  Isn't the sort algorithm a generic template function that will work with any random access container?
You are correct about the STL sort algorithm nietod, it work just fine on vectors and anything else that has appropriate iterators.

std::list does, however have a sort method defined on it, which vector does not, maybe that is what jkr is referring too.

you could use the STL sort algorithm to sort a simple C array too.
>> std::list does, however have a sort method
>> defined on it, which vector does not
That's was probably the problem.  I wonder why they didn't put a sort in both?

>> you could use the STL sort algorithm to sort a
>> simple C array too.
That's what I thought.  as far as the template is concerned, a pointer to an array item is the same as an iterator to a vector or list item..
> I wonder why they didn't put a sort in both?

I think its because the std::vector doesn't need one.  vector has a random access iterator which means that the standard algorithm will work just fine.

list only has a forward iterator I think, so, std::sort will not work.


(BTW, see the horror of the compiler errors when you compile the following code with VC++ at least:

void main()
{
    std::list<int> l1;
    std::sort(l1.begin(), l1.end());
}

)

Avatar of Embt

ASKER

Thanks for the discussion, I ended up with using vector and doing the sorting, it works fine for the vector. however, it did not completely solve my problem. the actual thing I want to do is not just sorting the array(or vector), it is like the following.

1. I have a set of (string, float) value, I can either use map or array of structures to store them. if I use map, the key(string part) would be unique.

2. I need to sort the float value in the above in desceding order, and get rid of "0" value.

How do I do this?  can somebody give me some sample code? I would increase the points if needed. or do I need to repost it? I am not sure
ASKER CERTIFIED SOLUTION
Avatar of nietod
nietod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hmm, never adding the zer0 entries is a clever solution.  Otherwise, use erase_if();
Here is an example of using the STL with vectors to do what you want.  The remove_if, erase combination achieves the removal of the zeros is they are already there.

#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <iostream>

using namespace std;

struct myPair
{
    myPair(float v, const string& s)
        : mValue(v), mString(s) {}

    float       mValue;
    string      mString;

    class MatchValue : public binary_function<myPair, int, bool>
    {
    public:
        bool operator()(const myPair& match, int value) const
        { return match.mValue == value; }
    };

    bool operator<(const myPair& rhs)
    { return mValue < rhs.mValue; }
};

ostream& operator<<(ostream& o, const myPair& mp)
{
    o << "[" << mp.mValue << ", " << mp.mString << "]";
    return o;
}

void main()
{
    // Create and populate the vector
    vector<myPair> v;

    v.push_back(myPair(2,"a"));
    v.push_back(myPair(5,"b"));
    v.push_back(myPair(0,"c"));
    v.push_back(myPair(3,"d"));
    v.push_back(myPair(0,"e"));

    copy(v.begin(), v.end(), ostream_iterator<myPair>(cout, " "));
    cout << endl;

    // Sort it
    sort(v.begin(), v.end());  

    copy(v.begin(), v.end(), ostream_iterator<myPair>(cout, " "));
    cout << endl;

    // Remove the zeros
    vector<myPair>::iterator newEnd = remove_if(v.begin(), v.end(),
                                                bind2nd(myPair::MatchValue(), 0));

    v.erase(newEnd,v.end());

    copy(v.begin(), v.end(), ostream_iterator<myPair>(cout, " "));
    cout << endl;
}
Avatar of Embt

ASKER

Thanks all for the discussion, the issue is going to be closed. it is very hard to decide to give the credits to which one of you since you are all very helpful. Thanks!