Ah yes, sorry. For some reason I believed the program needed to produce unique numbers. Well, this si what happens if you just glance over the specifications.
Now before providing another solution using arrays, I just have one more question. Can you use preimplemented algorithms or do you need to write a sorting function as well? If so, are there any runtime bounds?
-----------------------------------------------------------
Does it need to be an array? If not, here is a very nice and very fast way of doing just what you need.
Insertion O(n lg n); ( for n {= 10} elements )
Occurence checking: O (m) where m = # different items
Output (no repeating elements) O(m);
Output ( with repeating elements) O (n)
So here we go:
#include
#include
#define ITEMS 10
using namespace std;
int
main( int argc, char * args[] )
{
//Storage for numbers
multiset < int > values;
//Get input
for ( int i =0; i< ITEMS; i++ )
{
int number;
cout << "Well? : "<< flush;
cin >> number;
values.insert( number );
}
//Generate sorted, ascending, unique output
//For descending you would use rbegin();
multiset< int > :: iterator I_value;
I_value = values.begin();
while ( I_value != values.end() )
{
cout <<"Unique Sorted: "<<(*I_value) << "\n" << flush;
I_value = values.upper_bound( *I_value );
}
//Output all numbers in ascending order
for (I_value = values.begin(); I_value != values.end(); I_value ++)
{
cout <<"Just Sorted: "<<(*I_value) << "\n" << flush;
}
return 0;
}